P4C
The P4 Compiler
Loading...
Searching...
No Matches
tofino/bf-p4c/logging/logging.h
1
19#ifndef _BACKENDS_TOFINO_BF_P4C_LOGGING_LOGGING_H_
20#define _BACKENDS_TOFINO_BF_P4C_LOGGING_LOGGING_H_
21
22#include <string.h>
23#include <time.h>
24
25#include <cstdarg>
26#include <fstream>
27
28#include "bf-p4c/common/run_id.h"
29#include "rapidjson_adapter.h"
30
31namespace Logging {
33enum LogLevel_t { LOG, DEBUG, INFO, WARNING, ERROR, CRITICAL };
34
36class Logger : public rapidjson::Document {
37 private:
38 LogLevel_t _level;
39 std::ofstream _logFile;
40
41 public:
42 explicit Logger(const char *filename, LogLevel_t level = INFO)
43 : rapidjson::Document(rapidjson::Type::kObjectType), _level(level) {
44 _logFile.open(filename, std::ofstream::out);
45 }
46 ~Logger() {
47 _logFile.flush();
48 _logFile.close();
49 }
50
51 void setLevel(LogLevel_t level) { _level = level; }
52 virtual void serialize(Writer &) const = 0;
53 virtual void log() {
54 rapidjson::StringBuffer sb;
55 PrettyWriterAdapter writerAdapter(sb);
56 serialize(writerAdapter);
57 _logFile << sb.GetString();
58 _logFile.flush();
59 }
60
61 static const std::string buildDate(void) {
62 const time_t now = time(NULL);
63 struct tm tmp;
64 if (localtime_r(&now, &tmp) == NULL) {
65 throw std::runtime_error("Error calling localtime_r: " + std::string(strerror(errno)));
66 }
67
68 char bdate[1024];
69 strftime(bdate, 1024, "%c", &tmp);
70 return bdate;
71 }
72};
73} // end namespace Logging
74
75#endif /* _BACKENDS_TOFINO_BF_P4C_LOGGING_LOGGING_H_ */
Base class for logging to a file.
Definition tofino/bf-p4c/logging/logging.h:36
Definition rapidjson_adapter.h:51
Definition rapidjson_adapter.h:36
Definition filelog.h:32
LogLevel_t
Define the levels of logging. All messages above the set level for logger are logged.
Definition tofino/bf-p4c/logging/logging.h:33