logkit.hpp
Go to the documentation of this file.
1// $Id: logkit.hpp 1072 2012-09-18 13:53:37Z perroe $
2
3// Copyright (c) 2011, Norwegian Computing Center
4// All rights reserved.
5// Redistribution and use in source and binary forms, with or without modification,
6// are permitted provided that the following conditions are met:
7// • Redistributions of source code must retain the above copyright notice, this
8// list of conditions and the following disclaimer.
9// • Redistributions in binary form must reproduce the above copyright notice, this list of
10// conditions and the following disclaimer in the documentation and/or other materials
11// provided with the distribution.
12// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
13// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
14// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
15// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
16// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
17// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
19// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
20// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21
22#ifndef NRLIB_LOGKIT_H
23#define NRLIB_LOGKIT_H
24
25#include<ostream>
26#include<vector>
27#include<string>
28
29namespace NRLib {
30
31class LogStream;
32struct BufferMessage;
33
35class LogKit {
36public:
49
51 enum MessageLevels {Error = 1, Warning = 2, Low = 4, Medium = 8, High = 16, DebugLow = 32, DebugHigh = 64};
52
54 enum LimitLevels {L_Error = 1, L_Warning = 3, L_Low = 7, L_Medium = 15,
55 L_High = 31, L_DebugLow = 63, L_DebugHigh = 127};
56
58 static void SetFileLog(const std::string & fileName, int levels,
59 bool includeNRLibLogging = true);
60
62 static void SetFileLog(const std::string & fileName,
63 const std::vector<int> & levels,
64 bool ignore_general = false);
65
67 static void SetFileLog(const std::string & fileName,
68 int levels,
69 int phase,
70 bool ignore_general = false);
71
72
74 static void SetScreenLog(int levels, bool includeNRLibLogging = true);
75
77 static void SetScreenLog(const std::vector<int> & levels, bool ignore_general = false);
78
79
81 static void LogMessage(int level, const std::string & message);
82
84 static void LogMessage(int level, int phase, const std::string & message);
85
87 // Sending format as reference fails.
88 static void LogFormatted(int level, std::string format, ...);
89
91 static void LogFormatted(int level, int phase, std::string format, ...);
92
93
95 static void EndLog();
96
100 static void StartBuffering();
101 static void EndBuffering();
102
103 static void SetPrefix(const std::string & prefix, int level);
104 static int GetNMessages(int level) { return n_messages_[level];}
105 static void WriteHeader(const std::string & text, MessageLevels logLevel = Low);
106
107
108private:
109 static std::vector<LogStream *> logstreams_;
110 static int screenLog_; //Remembers which log is screen.
111 static std::vector<BufferMessage *> * buffer_;
112 static std::vector<int> n_messages_;
113 static std::vector<std::string> prefix_;
114
115 static void SendToBuffer(int level, int phase, const std::string & message);
116 static void DumpBuffer(LogStream * logstream);
117};
118
121public:
123 LogStream(std::ostream * logstream, int level);
124 LogStream(std::ostream * logstream, const std::vector<int> & levels, bool ignore_general = false);
126
127 void LogMessage(int level, const std::string & message);
128 void LogMessage(int level, int phase, const std::string & message);
129
130private:
131 std::ostream * logstream_;
132 std::vector<int> levels_;
133 int fullLevel_;
134 bool deleteStream;
135};
136
141};
142
143}
144#endif
145
char const int const cJSON_bool format
Definition: cJSON.h:161
const char *const string
Definition: cJSON.h:170
Kit for logging of messages from program.
Definition: logkit.hpp:35
MessageLevels
Symbols for use when sending message level and parsing exact levels.
Definition: logkit.hpp:51
@ Error
Definition: logkit.hpp:51
@ High
Definition: logkit.hpp:51
@ Low
Definition: logkit.hpp:51
@ DebugHigh
Definition: logkit.hpp:51
@ Warning
Definition: logkit.hpp:51
@ DebugLow
Definition: logkit.hpp:51
@ Medium
Definition: logkit.hpp:51
static void WriteHeader(const std::string &text, MessageLevels logLevel=Low)
static void SetFileLog(const std::string &fileName, const std::vector< int > &levels, bool ignore_general=false)
Set a full phase dependent file log.
static int GetNMessages(int level)
Definition: logkit.hpp:104
LimitLevels
Symbols for use when parsing given level and lower.
Definition: logkit.hpp:54
@ L_DebugHigh
Definition: logkit.hpp:55
@ L_Error
Definition: logkit.hpp:54
@ L_Medium
Definition: logkit.hpp:54
@ L_Low
Definition: logkit.hpp:54
@ L_DebugLow
Definition: logkit.hpp:55
@ L_Warning
Definition: logkit.hpp:54
@ L_High
Definition: logkit.hpp:55
static void EndLog()
Close streams.
static void SetScreenLog(const std::vector< int > &levels, bool ignore_general=false)
Set a full phase dependent screen log.
static void SetFileLog(const std::string &fileName, int levels, int phase, bool ignore_general=false)
Set single-phase file log, useful for debugging given phase.
static void EndBuffering()
static void LogMessage(int level, int phase, const std::string &message)
Send message in given phase.
static void SetFileLog(const std::string &fileName, int levels, bool includeNRLibLogging=true)
Set a file that logs independent of phase.
static void SetPrefix(const std::string &prefix, int level)
static void LogFormatted(int level, std::string format,...)
Send message as c-style format string and arguments.
static void LogFormatted(int level, int phase, std::string format,...)
Send message as c-style format string and arguments.
static void StartBuffering()
static void LogMessage(int level, const std::string &message)
Send message independent of phase.
static void SetScreenLog(int levels, bool includeNRLibLogging=true)
Set a screen log independent of phase.
Class LogStream is for internal use in LogKit only.
Definition: logkit.hpp:120
LogStream(std::ostream *logstream, const std::vector< int > &levels, bool ignore_general=false)
void LogMessage(int level, int phase, const std::string &message)
LogStream(std::ostream *logstream, int level)
Convention: logstream = NULL means cout.
void LogMessage(int level, const std::string &message)
Definition: exception.hpp:31
Definition: logkit.hpp:137
int level_
Definition: logkit.hpp:140
std::string text_
Definition: logkit.hpp:138
int phase_
Definition: logkit.hpp:139