SubZero Common
Common library components for an FRC CommandRobot
Loading...
Searching...
No Matches
ILogger.h
Go to the documentation of this file.
1#pragma once
2
3#include <fmt/format.h>
4#include <frc/geometry/Pose2d.h>
5#include <wpi/json.h>
6#include <wpi/sendable/Sendable.h>
7
8#include <cstdarg>
9#include <string>
10
12
13namespace subzero {
14
19class ILogger {
20public:
21 // string
22 virtual void logVerbose(std::string key, const std::string format, ...) = 0;
23 virtual void logInfo(std::string key, const std::string format, ...) = 0;
24 virtual void logWarning(std::string key, const std::string format, ...) = 0;
25 virtual void logError(std::string key, const std::string format, ...) = 0;
26 virtual void logFatal(std::string key, const std::string format, ...) = 0;
27
28 // int
29 virtual void logVerbose(std::string key, int val) = 0;
30 virtual void logInfo(std::string key, int val) = 0;
31 virtual void logWarning(std::string key, int val) = 0;
32 virtual void logError(std::string key, int val) = 0;
33 virtual void logFatal(std::string key, int val) = 0;
34
35 // double
36 virtual void logVerbose(std::string key, double val) = 0;
37 virtual void logInfo(std::string key, double val) = 0;
38 virtual void logWarning(std::string key, double val) = 0;
39 virtual void logError(std::string key, double val) = 0;
40 virtual void logFatal(std::string key, double val) = 0;
41
42 // bool
43 virtual void logInfo(std::string key, bool val) = 0;
44 virtual void logVerbose(std::string key, bool val) = 0;
45 virtual void logWarning(std::string key, bool val) = 0;
46 virtual void logError(std::string key, bool val) = 0;
47 virtual void logFatal(std::string key, bool val) = 0;
48
49 // Pose2d
50 virtual void logInfo(std::string key, frc::Pose2d &val) = 0;
51 virtual void logVerbose(std::string key, frc::Pose2d &val) = 0;
52 virtual void logWarning(std::string key, frc::Pose2d &val) = 0;
53 virtual void logError(std::string key, frc::Pose2d &val) = 0;
54 virtual void logFatal(std::string key, frc::Pose2d &val) = 0;
55
56 // Sendable
57 virtual void logInfo(std::string key, wpi::Sendable *val) = 0;
58 virtual void logVerbose(std::string key, wpi::Sendable *val) = 0;
59 virtual void logWarning(std::string key, wpi::Sendable *val) = 0;
60 virtual void logError(std::string key, wpi::Sendable *val) = 0;
61 virtual void logFatal(std::string key, wpi::Sendable *val) = 0;
62
63protected:
64 std::string levelToString(Logging::Level level) const {
65 using namespace Logging;
66
67 switch (level) {
68 case Level::VERBOSE:
69 return "VERBOSE";
70 case Level::INFO:
71 return "INFO";
72 case Level::WARNING:
73 return "WARNING";
74 case Level::ERROR:
75 return "ERROR";
76 case Level::FATAL:
77 return "FATAL";
78 default:
79 return "INVALID LOG LEVEL";
80 }
81 }
82
83 std::string formatString(const std::string format, va_list ap) {
84 char buf[1000];
85 vsnprintf(buf, sizeof(buf), format.c_str(), ap);
86
87 return std::string(buf);
88 }
89
90 std::string poseToString(const frc::Pose2d &pose) {
91 wpi::json json;
92
93 frc::to_json(json, pose);
94 return json.dump();
95 }
96
97 inline bool shouldLog(Logging::Level level) {
98 return static_cast<int>(level) >= static_cast<int>(Logging::kMinLogLevel);
99 }
100};
101} // namespace subzero
Logger interface.
Definition ILogger.h:19
virtual void logVerbose(std::string key, bool val)=0
virtual void logFatal(std::string key, const std::string format,...)=0
virtual void logVerbose(std::string key, double val)=0
virtual void logVerbose(std::string key, int val)=0
virtual void logError(std::string key, int val)=0
virtual void logError(std::string key, const std::string format,...)=0
virtual void logInfo(std::string key, bool val)=0
virtual void logError(std::string key, wpi::Sendable *val)=0
virtual void logFatal(std::string key, int val)=0
bool shouldLog(Logging::Level level)
Definition ILogger.h:97
virtual void logError(std::string key, double val)=0
std::string poseToString(const frc::Pose2d &pose)
Definition ILogger.h:90
virtual void logError(std::string key, frc::Pose2d &val)=0
virtual void logFatal(std::string key, wpi::Sendable *val)=0
virtual void logFatal(std::string key, frc::Pose2d &val)=0
std::string levelToString(Logging::Level level) const
Definition ILogger.h:64
virtual void logInfo(std::string key, int val)=0
virtual void logVerbose(std::string key, wpi::Sendable *val)=0
virtual void logVerbose(std::string key, frc::Pose2d &val)=0
virtual void logInfo(std::string key, frc::Pose2d &val)=0
virtual void logFatal(std::string key, bool val)=0
virtual void logInfo(std::string key, double val)=0
virtual void logWarning(std::string key, bool val)=0
virtual void logFatal(std::string key, double val)=0
virtual void logVerbose(std::string key, const std::string format,...)=0
virtual void logWarning(std::string key, const std::string format,...)=0
virtual void logWarning(std::string key, frc::Pose2d &val)=0
virtual void logInfo(std::string key, wpi::Sendable *val)=0
virtual void logError(std::string key, bool val)=0
virtual void logWarning(std::string key, wpi::Sendable *val)=0
virtual void logWarning(std::string key, int val)=0
virtual void logWarning(std::string key, double val)=0
virtual void logInfo(std::string key, const std::string format,...)=0
std::string formatString(const std::string format, va_list ap)
Definition ILogger.h:83
Level
Levels of logging.
constexpr auto kMinLogLevel
Will not log messages that fall below this level.