forked from acts-project/acts
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlogging.cpp
More file actions
164 lines (133 loc) · 5.58 KB
/
logging.cpp
File metadata and controls
164 lines (133 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// This file is part of the ACTS project.
//
// Copyright (C) 2016 CERN for the benefit of the ACTS project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
#include "Acts/Utilities/Logger.hpp"
#include <fstream>
#include <memory>
// Helper functions that take logger as const ref argument
void processSomething(int value,
const Acts::Logger &logger = Acts::getDummyLogger()) {
ACTS_DEBUG("Processing value: " << value);
ACTS_INFO("Process completed successfully");
}
void optionalLogging(const Acts::Logger &logger = Acts::getDummyLogger()) {
ACTS_VERBOSE("This message will be discarded with dummy logger");
// Dummy logger discards all output - useful for performance-critical code
}
int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) {
//! [Member Logger Pattern]
struct MyClass {
std::unique_ptr<const Acts::Logger> m_logger;
const Acts::Logger &logger() const { return *m_logger; }
explicit MyClass(std::unique_ptr<const Acts::Logger> logger)
: m_logger(std::move(logger)) {}
void doWork() { ACTS_INFO("Doing work in MyClass"); }
};
// Usage
MyClass obj(Acts::getDefaultLogger("MyClass", Acts::Logging::INFO));
obj.doWork();
//! [Member Logger Pattern]
//! [Const Ref Argument Pattern]
// Usage with custom logger
auto customLogger = Acts::getDefaultLogger("Processor", Acts::Logging::DEBUG);
processSomething(42, *customLogger);
// Usage with default (dummy) logger
processSomething(100);
//! [Const Ref Argument Pattern]
//! [getDummyLogger Pattern]
// Call without logging overhead
optionalLogging();
// Or provide a real logger when needed
auto debugLogger = Acts::getDefaultLogger("Debug", Acts::Logging::VERBOSE);
optionalLogging(*debugLogger);
//! [getDummyLogger Pattern]
{
auto verboseLogger =
Acts::getDefaultLogger("ComponentB", Acts::Logging::VERBOSE);
const auto &logger = *verboseLogger;
int variable = 10;
std::string errorMsg = "File not found";
//! [Logging Macros]
ACTS_VERBOSE("Detailed trace information");
ACTS_DEBUG("Debug info: " << variable);
ACTS_INFO("Operation completed");
ACTS_WARNING("Potential issue detected");
ACTS_ERROR("Operation failed: " << errorMsg);
ACTS_FATAL("Critical failure");
//! [Logging Macros]
}
//! [Logger Cloning]
// Create a base logger
auto baseLogger = Acts::getDefaultLogger("Fitter", Acts::Logging::INFO);
// Clone with same name and level
auto clonedLogger = baseLogger->clone();
// Clone with a different name
auto renamedLogger = baseLogger->clone("NewFitter");
// Clone with a different log level (keeps same name)
auto verboseClone = baseLogger->clone(Acts::Logging::VERBOSE);
// Clone with both new name and new level
auto customClone = baseLogger->clone("CustomFitter", Acts::Logging::DEBUG);
// Clone with a suffix appended to the name
auto actorLogger = baseLogger->cloneWithSuffix("Actor");
// Result: logger named "FitterActor" with INFO level
// Clone with a suffix and different level
auto debugActorLogger =
baseLogger->cloneWithSuffix("Actor", Acts::Logging::DEBUG);
// Result: logger named "FitterActor" with DEBUG level
// Common pattern: Create sub-component loggers
auto updaterLogger = baseLogger->cloneWithSuffix("Updater");
auto smootherLogger = baseLogger->cloneWithSuffix("Smoother");
//! [Logger Cloning]
//! [Logger Cloning Sub-component]
// Example: Class with multiple internal components that need separate logging
struct TrackFitter {
std::unique_ptr<const Acts::Logger> m_logger;
std::unique_ptr<const Acts::Logger> m_actorLogger;
std::unique_ptr<const Acts::Logger> m_updaterLogger;
explicit TrackFitter(std::unique_ptr<const Acts::Logger> logger)
: m_logger(std::move(logger)),
m_actorLogger(m_logger->cloneWithSuffix("Actor")),
m_updaterLogger(m_logger->cloneWithSuffix("Updater")) {}
};
// Creates loggers: "Fitter", "FitterActor", "FitterUpdater"
//! [Logger Cloning Sub-component]
//! [Logger Cloning Per-component Levels]
// Creating loggers with different verbosity levels for different components
auto baseDetectorLogger =
Acts::getDefaultLogger("Detector", Acts::Logging::INFO);
auto surfaceLogger =
baseDetectorLogger->clone("Surface", Acts::Logging::DEBUG);
auto volumeLogger =
baseDetectorLogger->clone("Volume", Acts::Logging::WARNING);
//! [Logger Cloning Per-component Levels]
//! [Logger Cloning Testing]
// Creating test loggers with specific configurations
auto productionLogger =
Acts::getDefaultLogger("Production", Acts::Logging::WARNING);
auto testLogger = productionLogger->clone("Test", Acts::Logging::VERBOSE);
//! [Logger Cloning Testing]
//! [Custom Output Streams]
// Create a logger that writes to a file instead of stdout
std::ofstream logFile("mylog.txt");
auto fileLogger =
Acts::getDefaultLogger("Component", Acts::Logging::INFO, &logFile);
//! [Custom Output Streams]
}
//! [Local logger macro]
void myFunction() {
auto myLogger = Acts::getDefaultLogger("Production", Acts::Logging::WARNING);
ACTS_LOCAL_LOGGER(std::move(myLogger));
ACTS_VERBOSE("hello world!");
}
//! [Local logger macro]
//! [Logger preload]
namespace Acts {
std::unique_ptr<const Logger> getDefaultLogger(const std::string &,
const Logging::Level &,
std::ostream *);
}
//! [Logger preload]