P4C
The P4 Compiler
Loading...
Searching...
No Matches
p4tools/common/lib/logging.h
1/*
2 * SPDX-FileCopyrightText: 2023 The P4 Language Consortium
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef BACKENDS_P4TOOLS_COMMON_LIB_LOGGING_H_
8#define BACKENDS_P4TOOLS_COMMON_LIB_LOGGING_H_
9
10#include <filesystem>
11#include <optional>
12#include <string>
13#include <utility>
14
15#include <boost/format.hpp>
16
17#include "lib/log.h"
18
19namespace P4::P4Tools {
20
22inline std::string logHelper(boost::format &f) { return f.str(); }
23
25template <class T, class... Args>
26std::string logHelper(boost::format &f, T &&t, Args &&...args) {
27 return logHelper(f % std::forward<T>(t), std::forward<Args>(args)...);
28}
29
32// https://stackoverflow.com/a/25859856
33template <typename... Arguments>
34void printFeature(const std::string &label, int level, const std::string &fmt,
35 Arguments &&...args) {
36 // Do not print logging messages when logging is not enabled.
37 if (!Log::fileLogLevelIsAtLeast(label.c_str(), level)) {
38 return;
39 }
40
41 boost::format f(fmt);
42 LOG_FEATURE(label.c_str(), level, logHelper(f, std::forward<Arguments>(args)...));
43}
44
47template <typename... Arguments>
48void printInfo(const std::string &fmt, Arguments &&...args) {
49 printFeature("tools_info", 4, fmt, std::forward<Arguments>(args)...);
50}
51
55template <typename... Arguments>
56void printDebug(const std::string &fmt, Arguments &&...args) {
57 printFeature("tools_debug", 4, fmt, std::forward<Arguments>(args)...);
58}
59
62
65
68void printPerformanceReport(const std::optional<std::filesystem::path> &basePath = std::nullopt);
69
70} // namespace P4::P4Tools
71
72#endif /* BACKENDS_P4TOOLS_COMMON_LIB_LOGGING_H_ */
Definition common/compiler/compiler_result.cpp:7
std::string logHelper(boost::format &f)
Helper function for @printFeature.
Definition p4tools/common/lib/logging.h:22
void enablePerformanceLogging()
Enable printing of timing reports.
Definition common/lib/logging.cpp:18
void enableInformationLogging()
Enable the printing of basic run information.
Definition common/lib/logging.cpp:16
void printDebug(const std::string &fmt, Arguments &&...args)
Definition p4tools/common/lib/logging.h:56
void printFeature(const std::string &label, int level, const std::string &fmt, Arguments &&...args)
Definition p4tools/common/lib/logging.h:34
void printInfo(const std::string &fmt, Arguments &&...args)
Definition p4tools/common/lib/logging.h:48
void printPerformanceReport(const std::optional< std::filesystem::path > &basePath)
Definition common/lib/logging.cpp:20