-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathRlogFile.cpp
More file actions
233 lines (188 loc) · 5.84 KB
/
RlogFile.cpp
File metadata and controls
233 lines (188 loc) · 5.84 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// $Id: RlogFile.cpp 1186 2019-07-12 17:49:59Z mueller $
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright 2011-2018 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
// Revision History:
// Date Rev Version Comment
// 2018-12-19 1090 1.2.4 use RosPrintf(bool)
// 2018-12-18 1089 1.2.3 use c++ style casts
// 2018-12-17 1085 1.2.2 use std::lock_guard instead of boost
// 2017-03-04 858 2.2.1 use clock_gettime instead of gettimeofday
// 2015-01-08 631 2.2 Open(): now with RerrMsg and cout/cerr support
// 2014-12-10 611 2.1.2 timestamp now usec precision (was msec)
// 2013-10-11 539 2.1.1 fix date print (month was off by one)
// 2013-02-23 492 2.1 add Name(), keep log file name; add Dump()
// 2013-02-22 491 2.0 add Write(),IsNew(), RlogMsg iface; use lockable
// 2011-01-30 357 1.0 Initial version
// ---------------------------------------------------------------------------
/*!
\brief Implemenation of RlogFile.
*/
#include <time.h>
#include <errno.h>
#include <iostream>
#include "RosFill.hpp"
#include "RosPrintf.hpp"
#include "RlogMsg.hpp"
#include "RlogFile.hpp"
using namespace std;
/*!
\class Retro::RlogFile
\brief FIXME_docs
*/
// all method definitions in namespace Retro
namespace Retro {
//------------------------------------------+-----------------------------------
//! Default constructor
RlogFile::RlogFile()
: fpExtStream(nullptr),
fIntStream(),
fNew(true),
fName(),
fMutex()
{
ClearTime();
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
RlogFile::RlogFile(std::ostream* os, const std::string& name)
: fpExtStream(os),
fIntStream(),
fNew(false),
fName(BuildinStreamName(os, name)),
fMutex()
{
ClearTime();
}
//------------------------------------------+-----------------------------------
//! Destructor
RlogFile::~RlogFile()
{}
//------------------------------------------+-----------------------------------
//! FIXME_docs
bool RlogFile::Open(std::string name, RerrMsg& emsg)
{
std::ostream* os = nullptr;
if (name == "<cout>" || name == "-") os = &cout;
else if (name == "<cerr>") os = &cerr;
else if (name == "<clog>") os = &clog;
if (os) {
UseStream(os);
return true;
}
fNew = false;
fpExtStream = nullptr;
fName = name;
fIntStream.open(name.c_str());
if (!fIntStream.is_open())
emsg.InitErrno("RlogFile::Open",
string("open for '") + name + "' failed: ",
errno);
return fIntStream.is_open();
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
void RlogFile::Close()
{
fIntStream.close();
return;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
void RlogFile::UseStream(std::ostream* os, const std::string& name)
{
fNew = false;
if (fIntStream.is_open()) Close();
fpExtStream = os;
fName = BuildinStreamName(os, name);
return;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
void RlogFile::Write(const std::string& str, char tag)
{
ostream& os = fpExtStream ? *fpExtStream : fIntStream;
lock_guard<RlogFile> lock(*this);
if (tag) {
struct timespec ts;
::clock_gettime(CLOCK_REALTIME, &ts);
struct tm tymd;
::localtime_r(&ts.tv_sec, &tymd);
if (tymd.tm_year != fTagYear ||
tymd.tm_mon != fTagMonth ||
tymd.tm_mday != fTagDay) {
os << "-+- "
<< RosPrintf(tymd.tm_year+1900,"d",4) << "-"
<< RosPrintf(tymd.tm_mon+1,"d0",2) << "-"
<< RosPrintf(tymd.tm_mday,"d0",2) << " -+- \n";
fTagYear = tymd.tm_year;
fTagMonth = tymd.tm_mon;
fTagDay = tymd.tm_mday;
}
os << "-" << tag << "- "
<< RosPrintf(tymd.tm_hour,"d0",2) << ":"
<< RosPrintf(tymd.tm_min,"d0",2) << ":"
<< RosPrintf(tymd.tm_sec,"d0",2) << "."
<< RosPrintf(int(ts.tv_nsec)/1000,"d0",6) << " : ";
}
os << str;
if (str[str.length()-1] != '\n') os << endl;
return;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
void RlogFile::Dump(std::ostream& os, int ind, const char* text) const
{
RosFill bl(ind);
os << bl << (text?text:"--") << "RlogFile @ " << this << endl;
os << bl << " fpExtStream: " << fpExtStream << endl;
os << bl << " fIntStream.isopen " << fIntStream.is_open() << endl;
os << bl << " fNew " << RosPrintf(fNew) << endl;
os << bl << " fName " << fName << endl;
os << bl << " fTagYr,Mo,Dy " << fTagYear << ", " << fTagMonth
<< ", " << fTagDay << endl;
return;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
void RlogFile::lock()
{
fMutex.lock();
return;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
void RlogFile::unlock()
{
fMutex.unlock();
return;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
RlogFile& RlogFile::operator<<(const RlogMsg& lmsg)
{
string str = lmsg.String();
if (str.length() > 0) Write(str, lmsg.Tag());
return *this;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
void RlogFile::ClearTime()
{
fTagYear = -1;
fTagMonth = -1;
fTagDay = -1;
return;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
std::string RlogFile::BuildinStreamName(std::ostream* os,
const std::string& str)
{
if (str.size()) return str;
if (os == &cout) return string("<cout>");
if (os == &cerr) return string("<cerr>");
if (os == &clog) return string("<clog>");
return string("<?stream?>");
}
} // end namespace Retro