VOM fixes and logger improvements
[vpp.git] / src / vpp-api / vom / logger.hpp
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef __VOM_LOGGER_H__
17 #define __VOM_LOGGER_H__
18
19 #include <fstream>
20 #include <iostream>
21
22 #include "vom/enum_base.hpp"
23
24 namespace VOM {
25 struct log_level_t : enum_base<log_level_t>
26 {
27   const static log_level_t CRITICAL;
28   const static log_level_t ERROR;
29   const static log_level_t WARNING;
30   const static log_level_t INFO;
31   const static log_level_t DEBUG;
32
33 private:
34   /**
35    * Private constructor taking the value and the string name
36    */
37   log_level_t(int v, const std::string& s);
38 };
39
40 /**
41  * Ideally we'd use the boost logger but that is not prevelent
42  * in many distros. So something simple here instead.
43  */
44 class log_t
45 {
46 public:
47   /**
48    * Construct a logger
49    */
50   log_t();
51
52   /**
53    * Return the stream
54    */
55   std::ostream& stream(const char* file, int line, const log_level_t& level);
56
57   /**
58    * The configured level
59    */
60   const log_level_t& level() const;
61
62   /**
63    * set the logging level
64    */
65   void set(const log_level_t& level);
66
67   /**
68    * set a file to receive the logging data
69    */
70   void set(const std::string& ofile);
71
72 private:
73   /**
74    * the configured logging level
75    */
76   log_level_t m_level;
77
78   /**
79    * Opened file for debugging
80    */
81   std::ofstream m_file_stream;
82
83   /**
84    * Pointer to the output stream
85    */
86   std::ostream* m_o_stream;
87 };
88
89 /**
90  * Return a log object into which VPP objects can write
91  */
92 log_t& logger();
93
94 #define VOM_LOG(lvl)                                                           \
95   if (lvl >= logger().level())                                                 \
96   logger().stream(__FILE__, __LINE__, lvl)
97 };
98
99 /*
100  * fd.io coding-style-patch-verification: ON
101  *
102  * Local Variables:
103  * eval: (c-set-style "mozilla")
104  * End:
105  */
106
107 #endif