P4C
The P4 Compiler
Loading...
Searching...
No Matches
filelog.h
1
19#ifndef _BACKENDS_TOFINO_BF_P4C_LOGGING_FILELOG_H_
20#define _BACKENDS_TOFINO_BF_P4C_LOGGING_FILELOG_H_
21
22#include <sys/stat.h>
23
24#include <fstream>
25#include <streambuf>
26#include <string>
27#include <utility>
28
29#include "backends/tofino/bf-p4c/logging/manifest.h"
30#include "lib/cstring.h"
31
32namespace Logging {
33
35enum Mode {
38
41
43 AUTO
44};
45
47
48class FileLog {
49 private:
50 std::basic_streambuf<char> *clog_buff = nullptr;
51 std::ofstream *log = nullptr;
52
53 static const cstring &name2type(cstring logName);
54 static std::set<cstring> filesWritten;
55
56 public:
57 explicit FileLog(int pipe, cstring logName, Mode mode = AUTO) {
58 bool append = true;
59 switch (mode) {
60 case APPEND:
61 append = true;
62 break;
63 case CREATE:
64 append = false;
65 break;
66 case AUTO:
67 append = filesWritten.count(logName);
68 break;
69 }
70
71 filesWritten.insert(logName);
72
73 auto logDir = BFNContext::get().getOutputDirectory("logs"_cs, pipe);
74 if (logDir) {
75 // Assuming cstring has a method to convert to std::string
76 std::filesystem::path fullPath =
77 std::filesystem::path(logDir.string_view()) / logName.string_view();
78
79 // Open the file stream with the full path
80 LOG1("Open logfile " << fullPath.string() << " append: " << append);
81 Manifest::getManifest().addLog(pipe, name2type(logName), logName);
82 clog_buff = std::clog.rdbuf();
83 auto flags = std::ios_base::out;
84 if (append) flags |= std::ios_base::app;
85 log = new std::ofstream(fullPath, flags);
86 std::clog.rdbuf(log->rdbuf());
87 }
88 }
89
91 ~FileLog() { close(); }
92
94 void close() {
95 if (clog_buff != nullptr) {
96 std::clog.rdbuf(clog_buff);
97 }
98 if (log) {
99 log->flush();
100 log->close();
101 delete log;
102 log = nullptr;
103 }
104 }
105
107 static void close(FileLog *&log) {
108 if (!log) return;
109 delete log;
110 log = nullptr;
111 }
112};
113
114} // end namespace Logging
115
116#endif /* _BACKENDS_TOFINO_BF_P4C_LOGGING_FILELOG_H_ */
static BFNContext & get()
Definition bf-p4c-options.cpp:777
cstring getOutputDirectory(const cstring &suffix=cstring(), int pipe_id=-1)
Definition bf-p4c-options.cpp:795
A FileLog is used to redirect the logging output of a visitor pass to a file.
Definition filelog.h:48
~FileLog()
Closes the log using close.
Definition filelog.h:91
void close()
Untie the file and close the filestream.
Definition filelog.h:94
static void close(FileLog *&log)
Closes a FileLog and cleans up.
Definition filelog.h:107
static Manifest & getManifest()
Return the singleton object.
Definition manifest.cpp:414
Definition cstring.h:85
Definition filelog.h:32
Mode
specifies how the log file is created and/or reused.
Definition filelog.h:35
@ APPEND
Appends to a log. Creates a new log if it doesn't already exist.
Definition filelog.h:37
@ CREATE
Creates a new log. Overwrites if the log already exists.
Definition filelog.h:40
@ AUTO
Creates if this is the first time writing to the log; otherwise, appends.
Definition filelog.h:43