/usr/include/ticcutils/LogBuffer.h is in libticcutils2-dev 0.14-1+b1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | /*
Copyright (c) 2006 - 2016
CLST - Radboud University
ILK - Tilburg University
This file is part of ticcutils
ticcutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
ticcutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
For questions and suggestions, see:
https://github.com/LanguageMachines/ticcutils/issues
or send mail to:
lamasoftware (at ) science.ru.nl
*/
#ifndef TICC_LOGBUFFER_H
#define TICC_LOGBUFFER_H
#include <ctime>
#include <cstdio>
#include <string>
#include <cstring>
#include <typeinfo>
#include <iomanip>
#include <iostream>
#include <sys/time.h>
enum LogLevel{ LogSilent, LogNormal, LogDebug, LogHeavy, LogExtreme };
enum LogFlag { NoStamp=0, StampTime=1, StampMessage=2, StampBoth=3 };
template <class charT, class traits = std::char_traits<charT> >
class basic_log_buffer : public std::basic_streambuf<charT, traits> {
public:
basic_log_buffer( std::basic_ostream<charT,traits>& a,
const std::string& mess = "",
const LogFlag flag = StampBoth ):
ass_stream( &a ),
stamp_flag( flag ),
in_sync(true),
level(LogNormal),
threshold_level(LogNormal),
ass_mess( mess )
{};
~basic_log_buffer();
//
// setters/getters
LogLevel Level() const;
void Level( const LogLevel l );
LogLevel Threshold() const;
void Threshold( const LogLevel l );
const std::string& Message() const;
void Message( const std::string& );
std::basic_ostream<charT,traits>& AssocStream() const;
void AssocStream( std::basic_ostream<charT,traits>& );
LogFlag StampFlag() const;
void StampFlag( const LogFlag );
protected:
int sync();
int overflow( int );
private:
std::basic_ostream<charT,traits> *ass_stream;
LogFlag stamp_flag;
bool in_sync;
LogLevel level;
LogLevel threshold_level;
std::string ass_mess;
void buffer_out();
// prohibit copying and assignment
basic_log_buffer( const basic_log_buffer& );
basic_log_buffer& operator=( const basic_log_buffer& );
};
typedef basic_log_buffer<char, std::char_traits<char> > LogBuffer;
typedef basic_log_buffer<wchar_t, std::char_traits<wchar_t> > wLogBuffer;
template <class charT, class traits >
basic_log_buffer<charT,traits>::~basic_log_buffer(){
sync();
}
inline long millitm() {
struct timeval tp;
gettimeofday(&tp,NULL);
return tp.tv_usec/1000;
}
inline std::string time_stamp(){
char time_line[50];
time_t lTime;
struct tm *curtime;
time(&lTime);
struct tm tmp;
curtime = localtime_r(&lTime,&tmp);
strftime( time_line, 45, "%Y%m%d:%H%M%S", curtime );
sprintf( time_line+strlen(time_line), ":%03ld:", millitm() );
return time_line;
}
//
// for a derived output stream, we must provide implementations for
// both overflow and sync.
// both use a helper function buffer_out to do the real work.
//
template <class charT, class traits >
int basic_log_buffer<charT,traits>::overflow( int c ) {
buffer_out();
if ( level >= threshold_level && c != '\r' ){
// std::cerr << "overflow OK: " << level << " >= " << threshold_level
// << "(" << char(c) << ")" << std::endl;
if ( c != EOF ){
char z = static_cast<char>(c);
ass_stream->put( z );
}
else
return EOF;
}
return c;
}
template <class charT, class traits >
int basic_log_buffer<charT,traits>::sync() {
ass_stream->flush();
in_sync = true;
return 0;
}
template <class charT, class traits >
void basic_log_buffer<charT,traits>::buffer_out(){
if ( level >= threshold_level ){
// std::cerr << "buffer_out OK: " << level << " >= " << threshold_level << std::endl;
// only output when we are on a high enough level
if ( in_sync ) {
// stamps and messages are only displayed when in sync
// that is: when we have had a newline and NOT when we just
// overflowed due to a long line
if ( stamp_flag & StampTime ){
*ass_stream << time_stamp();
}
if ( !ass_mess.empty() && ( stamp_flag & StampMessage ) )
*ass_stream << ass_mess << ":";
in_sync = false;
}
}
}
//
// Getters and Setters for the private parts..
//
template <class charT, class traits >
const std::string& basic_log_buffer<charT,traits>::Message() const {
return ass_mess;
}
template <class charT, class traits >
void basic_log_buffer<charT,traits>::Message( const std::string& s ){
ass_mess = s;
}
template <class charT, class traits >
void basic_log_buffer<charT,traits>::Threshold( LogLevel l ){
if ( threshold_level != l ){
threshold_level = l;
}
}
template <class charT, class traits >
LogLevel basic_log_buffer<charT,traits>::Threshold() const {
return threshold_level;
}
template <class charT, class traits >
void basic_log_buffer<charT,traits>::Level( LogLevel l ){
if ( level != l ){
level = l;
}
}
template <class charT, class traits >
LogLevel basic_log_buffer<charT,traits>::Level() const {
return level;
}
template <class charT, class traits >
std::basic_ostream<charT,traits>& basic_log_buffer<charT,traits>::AssocStream() const {
return *ass_stream;
}
template <class charT, class traits >
void basic_log_buffer<charT,traits>::AssocStream( std::basic_ostream<charT,traits>& os ){
ass_stream = &os;
}
template <class charT, class traits >
void basic_log_buffer<charT,traits>::StampFlag( const LogFlag b ){
if ( stamp_flag != b ){
stamp_flag = b;
}
}
template <class charT, class traits >
LogFlag basic_log_buffer<charT,traits>::StampFlag() const {
return stamp_flag;
}
#endif // LOGBUFFER_H
|