Fix for SVACE and Klocwork issues.
[platform/upstream/iotivity.git] / service / simulator / inc / simulator_logger.h
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 /**
22  * @file   simulator_logger.h
23  *
24  * @brief   This file provides the interface for logging messages to different targets (console/file).
25  */
26
27 #ifndef SIMULATOR_LOGGER_H_
28 #define SIMULATOR_LOGGER_H_
29
30 #include <iostream>
31 #include <memory>
32
33 class ILogger
34 {
35     public:
36         enum Level
37         {
38             INFO = 0,
39             DEBUG,
40             WARNING,
41             ERROR
42         };
43
44         static const char *getString(Level level)
45         {
46             switch (level)
47             {
48                 case Level::INFO: return "INFO";
49                 case Level::DEBUG: return "DEBUG";
50                 case Level::WARNING: return "WARNING";
51                 case Level::ERROR: return "ERROR";
52                 default: return "UNKNOWN";
53             }
54         }
55
56         virtual void write(std::string, Level, std::string) = 0;
57 };
58
59 class Logger
60 {
61     public:
62         bool setDefaultConsoleTarget();
63         bool setDefaultFileTarget(const std::string &path);
64         void setCustomTarget(const std::shared_ptr<ILogger> &target);
65         void write(ILogger::Level level, std::ostringstream &str);
66
67     private:
68         std::shared_ptr<ILogger> m_target;
69 };
70
71 auto simLogger() -> Logger &;
72
73 #ifndef SIM_LOG
74 #define SIM_LOG(LEVEL, MSG) { \
75         simLogger().write(LEVEL, static_cast<std::ostringstream&>(std::ostringstream()<<MSG)); \
76     }
77 #endif
78
79 #endif