1 /*
2  * Copyright (c) 2014-2017, Siemens AG. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef EMBB_BASE_LOG_H_
28 #define EMBB_BASE_LOG_H_
29 
30 #include <embb/base/c/log.h>
31 
32 /**
33  * \defgroup CPP_LOG Logging
34  * \ingroup CPP_BASE
35  * Simple logging facilities.
36  */
37 
38 namespace embb {
39 namespace base {
40 
41 /**
42  * Simple logging facilities.
43  *
44  * \ingroup CPP_LOG
45  */
46 class Log {
47  private:
48   // do not allow construction
49   Log();
50 
51  public:
52   /**
53    * Sets the global log level.
54    * This determines what messages will be shown, messages with a more detailed
55    * log level will be filtered out. The default log level is EMBB_LOG_LEVEL_NONE.
56    * \notthreadsafe
57    */
58   static void SetLogLevel(
59     embb_log_level_t log_level           /**< [in] Log level to use for
60                                               filtering */
61   );
62 
63   /**
64    * Sets the global logging function.
65    * The logging function implements the mechanism for transferring log messages
66    * to their destination. \c context is a pointer to data the user needs in the
67    * function to determine where the messages should go (may be NULL if no
68    * additional data is needed). The default logging function is
69    * embb_log_write_file() with context set to \c stdout.
70    * \see embb_log_function_t
71    * \notthreadsafe
72    */
73   static void SetLogFunction(
74     void * context,                      /**< [in] User context to supply as the
75                                               first parameter of the logging
76                                               function*/
77     embb_log_function_t func             /**< [in] The logging function */
78   );
79 
80   /**
81    * Logs a message to the given channel with the specified log level.
82    * If the log level is greater than the configured log level for the channel,
83    * the message will be ignored.
84    * \see  embb::base::Log::SetLogLevel, embb::base::Log::SetLogFunction
85    * \threadsafe
86    */
87   static void Write(
88     char const * channel,                /**< [in] User specified channel id
89                                               for filtering the log later on.
90                                               Might be NULL, channel identifier
91                                               will be "global" in that case */
92     embb_log_level_t log_level,          /**< [in] Log level to use */
93     char const * message,                /**< [in] Message to convey, may use
94                                               \c printf style formatting */
95     ...                                  /**< Additional parameters determined
96                                               by the format specifiers in
97                                               \c message */
98   );
99 
100   /**
101    * Logs a message to the given channel with EMBB_LOG_LEVEL_TRACE.
102    * In non-debug builds, this function does nothing.
103    * \see embb::base::Log::Write
104    * \threadsafe
105    */
106   static void Trace(
107     char const * channel,                /**< [in] User specified channel id */
108     char const * message,                /**< [in] Message to convey, may use
109                                               \c printf style formatting */
110     ...                                  /**< Additional parameters determined
111                                               by the format specifiers in
112                                               \c message */
113   );
114 
115   /**
116    * Logs a message to the given channel with EMBB_LOG_LEVEL_INFO.
117    * In non-debug builds, this function does nothing.
118    * \see embb::base::Log::Write
119    * \threadsafe
120    */
121   static void Info(
122     char const * channel,                /**< [in] User specified channel id */
123     char const * message,                /**< [in] Message to convey, may use
124                                               \c printf style formatting */
125     ...                                  /**< Additional parameters determined
126                                               by the format specifiers in
127                                               \c message */
128   );
129 
130   /**
131    * Logs a message to the given channel with EMBB_LOG_LEVEL_WARNING.
132    * \see embb::base::Log::Write
133    * \threadsafe
134    */
135   static void Warning(
136     char const * channel,                /**< [in] User specified channel id */
137     char const * message,                /**< [in] Message to convey, may use
138                                               \c printf style formatting */
139     ...                                  /**< Additional parameters determined
140                                               by the format specifiers in
141                                               \c message */
142   );
143 
144   /**
145    * Logs a message to the given channel with EMBB_LOG_LEVEL_ERROR.
146    * \see embb::base::Log::Write
147    * \threadsafe
148    */
149   static void Error(
150     char const * channel,                /**< [in] User specified channel id */
151     char const * message,                /**< [in] Message to convey, may use
152                                               \c printf style formatting */
153     ...                                  /**< Additional parameters determined
154                                               by the format specifiers in
155                                               \c message */
156   );
157 };
158 
159 } // namespace base
160 } // namespace embb
161 
162 #endif // EMBB_BASE_LOG_H_
163