cppthread 1.1.16
C++ Thread Library
log.h
Go to the documentation of this file.
1// Copyright (c) 2013-2025 Made to Order Software Corp. All Rights Reserved
2//
3// https://snapwebsites.org/project/cppthread
4// contact@m2osw.com
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 2 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License along
17// with this program; if not, write to the Free Software Foundation, Inc.,
18// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19#pragma once
20
28// C++
29//
30#include <cstdint>
31#include <iostream>
32#include <sstream>
33
34
35namespace cppthread
36{
37
38
39enum class log_level_t
40{
41 debug,
42 info,
43 warning,
44 error,
45 fatal,
46
47 LOG_LEVEL_SIZE
48};
49
50std::string to_string(log_level_t level);
51
52
53typedef void (*log_callback)(log_level_t level, std::string const & message);
54
55void set_log_callback(log_callback callback);
56
57
58class logger final
59{
60public:
61 logger();
62
63 logger & end();
64 logger & operator << (log_level_t const & level);
65 logger & operator << (logger & (*func)(logger &));
66
67 template<typename T>
68 logger & operator << (T const & v)
69 {
70 lock();
71 f_log << v;
72 return *this;
73 }
74
75 void reset_counters();
76 std::uint32_t get_counter(log_level_t level) const;
77 std::uint32_t get_errors() const;
78 std::uint32_t get_warnings() const;
79
80private:
81 static void lock();
82 static void unlock();
83
84 log_level_t f_level = log_level_t::error;
85 std::stringstream f_log = std::stringstream();
86 std::uint32_t f_counters[static_cast<int>(log_level_t::LOG_LEVEL_SIZE)] = {};
87};
88
89
90inline logger & end(logger & l) { return l.end(); }
91
92
93extern logger log;
94
95
96} // namespace cppthread
97// vim: ts=4 sw=4 et
The cppthread logger.
Definition log.h:59
std::uint32_t get_errors() const
Get the number of errors that occurred so far.
Definition log.cpp:511
static void lock()
Lock the system so a log can be emitted properly.
Definition log.cpp:238
void reset_counters()
Reset all the log message counters to zero.
Definition log.cpp:457
std::uint32_t get_counter(log_level_t level) const
Get one of the level counters.
Definition log.cpp:483
logger & end()
End the logger's message.
Definition log.cpp:554
static void unlock()
Unlock the logger once we are done with it.
Definition log.cpp:346
logger()
Initialize the logger.
Definition log.cpp:207
logger & operator<<(log_level_t const &level)
Save the level at which to log this message.
Definition log.cpp:407
std::stringstream f_log
The log message.
Definition log.h:85
std::uint32_t get_warnings() const
Get the number of warnings that occurred so far.
Definition log.cpp:531
log_level_t f_level
The level of this message.
Definition log.h:84
void set_log_callback(log_callback callback)
Set a callback function.
Definition log.cpp:170
log_level_t
The log level or severity.
Definition log.h:40
logger & end(logger &l)
Close a log statement.
Definition log.h:90

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.