xref: /aosp_15_r20/external/openthread/src/posix/platform/logger.hpp (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1 /*
2  *  Copyright (c) 2024, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  *   This file implements the `Logger` class for use by POSIX platform module.
32  */
33 
34 #ifndef OT_POSIX_PLATFORM_LOGGER_HPP_
35 #define OT_POSIX_PLATFORM_LOGGER_HPP_
36 
37 #include "openthread-posix-config.h"
38 
39 #include <openthread/logging.h>
40 
41 namespace ot {
42 namespace Posix {
43 
44 /**
45  * Provides logging methods for a specific POSIX module.
46  *
47  * The `Type` class MUST provide a `static const char kLogModuleName[]` which specifies the POSIX log module name to
48  * include in the platform logs (using `otLogPlatArgs()`).
49  *
50  * Users of this class should follow CRTP-style inheritance, i.e., the `Type` class itself should inherit from
51  * `Logger<Type>`.
52  *
53  */
54 template <typename Type> class Logger
55 {
56 public:
57     /**
58      * Emits a log message at critical log level.
59      *
60      * @param[in]  aFormat  The format string.
61      * @param[in]  ...      Arguments for the format specification.
62      *
63      */
LogCrit(const char * aFormat,...)64     static void LogCrit(const char *aFormat, ...) OT_TOOL_PRINTF_STYLE_FORMAT_ARG_CHECK(1, 2)
65     {
66         va_list args;
67 
68         va_start(args, aFormat);
69         otLogPlatArgs(OT_LOG_LEVEL_CRIT, Type::kLogModuleName, aFormat, args);
70         va_end(args);
71     }
72 
73     /**
74      * Emits a log message at warning log level.
75      *
76      * @param[in]  aFormat  The format string.
77      * @param[in]  ...      Arguments for the format specification.
78      *
79      */
LogWarn(const char * aFormat,...)80     static void LogWarn(const char *aFormat, ...) OT_TOOL_PRINTF_STYLE_FORMAT_ARG_CHECK(1, 2)
81     {
82         va_list args;
83 
84         va_start(args, aFormat);
85         otLogPlatArgs(OT_LOG_LEVEL_WARN, Type::kLogModuleName, aFormat, args);
86         va_end(args);
87     }
88 
89     /**
90      * Emits a log message at note log level.
91      *
92      * @param[in]  aFormat  The format string.
93      * @param[in]  ...      Arguments for the format specification.
94      *
95      */
LogNote(const char * aFormat,...)96     static void LogNote(const char *aFormat, ...) OT_TOOL_PRINTF_STYLE_FORMAT_ARG_CHECK(1, 2)
97     {
98         va_list args;
99 
100         va_start(args, aFormat);
101         otLogPlatArgs(OT_LOG_LEVEL_NOTE, Type::kLogModuleName, aFormat, args);
102         va_end(args);
103     }
104 
105     /**
106      * Emits a log message at info log level.
107      *
108      * @param[in]  aFormat  The format string.
109      * @param[in]  ...      Arguments for the format specification.
110      *
111      */
LogInfo(const char * aFormat,...)112     static void LogInfo(const char *aFormat, ...) OT_TOOL_PRINTF_STYLE_FORMAT_ARG_CHECK(1, 2)
113     {
114         va_list args;
115 
116         va_start(args, aFormat);
117         otLogPlatArgs(OT_LOG_LEVEL_INFO, Type::kLogModuleName, aFormat, args);
118         va_end(args);
119     }
120 
121     /**
122      * Emits a log message at debug log level.
123      *
124      * @param[in]  aFormat  The format string.
125      * @param[in]  ...      Arguments for the format specification.
126      *
127      */
LogDebg(const char * aFormat,...)128     static void LogDebg(const char *aFormat, ...) OT_TOOL_PRINTF_STYLE_FORMAT_ARG_CHECK(1, 2)
129     {
130         va_list args;
131 
132         va_start(args, aFormat);
133         otLogPlatArgs(OT_LOG_LEVEL_DEBG, Type::kLogModuleName, aFormat, args);
134         va_end(args);
135     }
136 };
137 
138 } // namespace Posix
139 } // namespace ot
140 
141 #endif // OT_POSIX_PLATFORM_LOGGER_HPP_
142