SSP21-CPP
LogLevels.h
1 /*
2  * Copyright (c) 2019, Automatak LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
6  * following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
9  * disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
12  * disclaimer in the documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
15  * products derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #ifndef LOG4CPP_LOGLEVELS_H
26 #define LOG4CPP_LOGLEVELS_H
27 
28 #include <cstdint>
29 
30 namespace log4cpp
31 {
32 
33 struct ModuleId
34 {
35 public:
36  ModuleId() = default;
37 
38  explicit ModuleId(int32_t level) : value(level)
39  {}
40 
41  int32_t value = 0;
42 };
43 
44 struct LogLevel
45 {
46 public:
47  LogLevel() = default;
48 
49  explicit LogLevel(int32_t level) : value(level)
50  {}
51 
52  LogLevel next() const
53  {
54  return LogLevel(value << 1);
55  }
56 
57  int32_t value = 0;
58 };
59 
60 // some default log flags
61 namespace levels
62 {
63  const LogLevel event = LogLevel(1);
64  const LogLevel error = event.next();
65  const LogLevel warn = error.next();
66  const LogLevel info = warn.next();
67  const LogLevel debug = info.next();
68 }
69 
70 /**
71  * Strongly typed wrapper for flags bitfield
72  */
73 class LogLevels
74 {
75 public:
76  LogLevels() = default;
77 
78  explicit LogLevels(int32_t levels) : levels(levels)
79  {}
80 
81  static LogLevels everything()
82  {
83  return LogLevels(~0);
84  }
85 
86  inline bool is_set(const LogLevel& level) const
87  {
88  return (level.value & levels) != 0;
89  }
90 
91  LogLevels& operator|=(const LogLevels& other)
92  {
93  this->levels |= other.levels;
94  return *this;
95  }
96 
97  LogLevels operator|(const LogLevels& other) const
98  {
99  return LogLevels(this->levels | other.levels);
100  }
101 
102  inline int32_t get_value() const
103  {
104  return levels;
105  }
106 
107 private:
108  int32_t levels = 0;
109 };
110 
111 } //namespace log4cpp
112 
113 #endif //LOG4CPP_LOGLEVELS_H
log4cpp header-only library namespace
const log4cpp::LogLevel info
Information logs.
Definition: LogLevels.h:31
const log4cpp::LogLevel debug
Debug logs.
Definition: LogLevels.h:33
const log4cpp::LogLevel warn
Warning logs.
Definition: LogLevels.h:29
const log4cpp::LogLevel error
Error logs.
Definition: LogLevels.h:27