Gemmi C++ API
Loading...
Searching...
No Matches
logger.hpp
Go to the documentation of this file.
1// Copyright Global Phasing Ltd.
2//
3// Logger - a tiny utility for passing messages through a callback.
4
5#ifndef GEMMI_LOGGER_HPP_
6#define GEMMI_LOGGER_HPP_
7
8#include <cstdio> // for fprintf
9#include <functional> // for function
10#include "fail.hpp" // for GEMMI_COLD
11#include "util.hpp" // for cat
12
13namespace gemmi {
14
23struct Logger {
25 std::function<void(const std::string&)> callback;
28 int threshold = 6;
29
32 void suspend() { threshold -= 100; }
33 void resume() { threshold += 100; }
34
36 template<int N, class... Args> void level(Args const&... args) const {
37 if (threshold >= N && callback)
38 callback(cat(args...));
39 }
40
42 template<class... Args> void debug(Args const&... args) const { level<8>("Debug: ", args...); }
44 template<class... Args> void mesg(Args const&... args) const { level<6>(args...); }
46 template<class... Args> void note(Args const&... args) const { level<5>("Note: ", args...); }
47
49 template<class... Args> GEMMI_COLD void err(Args const&... args) const {
50 if (threshold >= 3) {
51 std::string msg = cat(args...);
52 if (callback == nullptr)
53 fail(msg);
54 callback("Warning: " + msg);
55 }
56 }
57
58 // predefined callbacks
59
61 static void to_stderr(const std::string& s) {
62 std::fprintf(stderr, "%s\n", s.c_str());
63 }
65 static void to_stdout(const std::string& s) {
66 std::fprintf(stdout, "%s\n", s.c_str());
67 }
68};
69
70} // namespace gemmi
71#endif
fail(), unreachable() and __declspec/__attribute__ macros
#define GEMMI_COLD
Definition fail.hpp:28
std::string cat(Args const &... args)
Definition util.hpp:33
void fail(const std::string &msg)
Definition fail.hpp:59
Passes messages (including warnings/errors) to a callback function.
Definition logger.hpp:23
static void to_stderr(const std::string &s)
to be used as: logger.callback = Logger::to_stderr;
Definition logger.hpp:61
void mesg(Args const &... args) const
Send a message without any prefix.
Definition logger.hpp:44
void suspend()
suspend() and resume() are used internally to avoid duplicate messages when the same function is call...
Definition logger.hpp:32
GEMMI_COLD void err(Args const &... args) const
Send a warning/error (see Quirk above).
Definition logger.hpp:49
static void to_stdout(const std::string &s)
to be used as: logger.callback = Logger::to_stdout;
Definition logger.hpp:65
void debug(Args const &... args) const
Send a debug message.
Definition logger.hpp:42
void level(Args const &... args) const
Send a message without any prefix on with a numeric threshold N.
Definition logger.hpp:36
int threshold
Pass messages of this level and all lower (more severe) levels: 8=all, 6=all but debug,...
Definition logger.hpp:28
void note(Args const &... args) const
Send a note (a notice, a significant message).
Definition logger.hpp:46
std::function< void(const std::string &)> callback
A function that handles messages.
Definition logger.hpp:25
void resume()
Definition logger.hpp:33
Utilities. Mostly for working with strings and vectors.