blob: 941623e667907b429033bea256193a2555ebb678 [file] [log] [blame]
Neale Ranns812ed392017-10-16 04:20:13 -07001/*
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
24namespace VOM {
25struct 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
33private:
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 */
44class log_t
45{
46public:
47 /**
48 * Construct a logger
49 */
50 log_t();
51
52 /**
53 * Return the stream
54 */
Neale Ranns8ac4ce82017-11-17 05:08:55 -080055 std::ostream& stream(const char* file, int line, const log_level_t& level);
Neale Ranns812ed392017-10-16 04:20:13 -070056
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
72private:
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 */
92log_t& logger();
93
94#define VOM_LOG(lvl) \
95 if (lvl >= logger().level()) \
Neale Ranns8ac4ce82017-11-17 05:08:55 -080096 logger().stream(__FILE__, __LINE__, lvl)
Neale Ranns812ed392017-10-16 04:20:13 -070097};
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