Skip to content

Commit

Permalink
Tracing: Create leading directories if they don't exist (#35668)
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq authored Sep 19, 2024
1 parent 7862cb3 commit afb1a33
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/controller/python/chip/tracing/TracingSetup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ chip::Tracing::Perfetto::PerfettoBackend gPerfettoBackend;

} // namespace

extern "C" void pychip_tracing_start_json_log(const char * file_name)
extern "C" void pychip_tracing_start_json_log()
{
chip::MainLoopWork::ExecuteInMainLoop([] {
gJsonBackend.CloseFile(); // just in case, ensure no file output
Expand Down
3 changes: 0 additions & 3 deletions src/controller/python/chip/tracing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ def StartFromString(self, destination: str):
else:
raise ValueError("Invalid trace-to destination: %r", destination)

def __init__(self):
pass

def __enter__(self):
return self

Expand Down
11 changes: 10 additions & 1 deletion src/tracing/json/json_tracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <errno.h>

#include <filesystem>
#include <sstream>
#include <string>

Expand Down Expand Up @@ -461,8 +462,16 @@ void JsonBackend::CloseFile()
CHIP_ERROR JsonBackend::OpenFile(const char * path)
{
CloseFile();
mOutputFile.open(path, std::ios_base::out);

std::error_code ec;
std::filesystem::path filePath(path);
// Create directories if they don't exist
if (!std::filesystem::create_directories(filePath.remove_filename(), ec))
{
return CHIP_ERROR_POSIX(ec.value());
}

mOutputFile.open(path, std::ios_base::out);
if (!mOutputFile)
{
return CHIP_ERROR_POSIX(errno);
Expand Down
9 changes: 9 additions & 0 deletions src/tracing/perfetto/file_output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <errno.h>
#include <fcntl.h>
#include <filesystem>

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

std::error_code ec;
std::filesystem::path filePath(file_name);
// Create directories if they don't exist
if (!std::filesystem::create_directories(filePath.remove_filename(), ec))
{
return CHIP_ERROR_POSIX(ec.value());
}

// Create a trace file and start sending data to it
mTraceFileId = open(file_name, O_RDWR | O_CREAT | O_TRUNC, 0640);
if (mTraceFileId < 0)
Expand Down

0 comments on commit afb1a33

Please sign in to comment.