Skip to content

Commit afb1a33

Browse files
authored
Tracing: Create leading directories if they don't exist (#35668)
1 parent 7862cb3 commit afb1a33

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

src/controller/python/chip/tracing/TracingSetup.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ chip::Tracing::Perfetto::PerfettoBackend gPerfettoBackend;
3434

3535
} // namespace
3636

37-
extern "C" void pychip_tracing_start_json_log(const char * file_name)
37+
extern "C" void pychip_tracing_start_json_log()
3838
{
3939
chip::MainLoopWork::ExecuteInMainLoop([] {
4040
gJsonBackend.CloseFile(); // just in case, ensure no file output

src/controller/python/chip/tracing/__init__.py

-3
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,6 @@ def StartFromString(self, destination: str):
115115
else:
116116
raise ValueError("Invalid trace-to destination: %r", destination)
117117

118-
def __init__(self):
119-
pass
120-
121118
def __enter__(self):
122119
return self
123120

src/tracing/json/json_tracing.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include <errno.h>
3333

34+
#include <filesystem>
3435
#include <sstream>
3536
#include <string>
3637

@@ -461,8 +462,16 @@ void JsonBackend::CloseFile()
461462
CHIP_ERROR JsonBackend::OpenFile(const char * path)
462463
{
463464
CloseFile();
464-
mOutputFile.open(path, std::ios_base::out);
465465

466+
std::error_code ec;
467+
std::filesystem::path filePath(path);
468+
// Create directories if they don't exist
469+
if (!std::filesystem::create_directories(filePath.remove_filename(), ec))
470+
{
471+
return CHIP_ERROR_POSIX(ec.value());
472+
}
473+
474+
mOutputFile.open(path, std::ios_base::out);
466475
if (!mOutputFile)
467476
{
468477
return CHIP_ERROR_POSIX(errno);

src/tracing/perfetto/file_output.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include <errno.h>
2626
#include <fcntl.h>
27+
#include <filesystem>
2728

2829
namespace chip {
2930
namespace Tracing {
@@ -37,6 +38,14 @@ CHIP_ERROR FileTraceOutput::Open(const char * file_name)
3738
// Close any existing files
3839
Close();
3940

41+
std::error_code ec;
42+
std::filesystem::path filePath(file_name);
43+
// Create directories if they don't exist
44+
if (!std::filesystem::create_directories(filePath.remove_filename(), ec))
45+
{
46+
return CHIP_ERROR_POSIX(ec.value());
47+
}
48+
4049
// Create a trace file and start sending data to it
4150
mTraceFileId = open(file_name, O_RDWR | O_CREAT | O_TRUNC, 0640);
4251
if (mTraceFileId < 0)

0 commit comments

Comments
 (0)