OpenCores
URL https://opencores.org/ocsvn/w11/w11/trunk

Subversion Repositories w11

[/] [w11/] [tags/] [w11a_V0.7/] [tools/] [src/] [librtools/] [RlogFile.cpp] - Blame information for rev 29

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 29 wfjm
// $Id: RlogFile.cpp 631 2015-01-09 21:36:51Z mueller $
2 10 wfjm
//
3 29 wfjm
// Copyright 2011-2015 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4 10 wfjm
//
5
// This program is free software; you may redistribute and/or modify it under
6
// the terms of the GNU General Public License as published by the Free
7
// Software Foundation, either version 2, or at your option any later version.
8
//
9
// This program is distributed in the hope that it will be useful, but
10
// WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
11
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
// for complete details.
13
// 
14
// Revision History: 
15
// Date         Rev Version  Comment
16 29 wfjm
// 2015-01-08   631   2.2    Open(): now with RerrMsg and cout/cerr support
17 27 wfjm
// 2014-12-10   611   2.1.2  timestamp now usec precision (was msec)
18 22 wfjm
// 2013-10-11   539   2.1.1  fix date print (month was off by one)
19 19 wfjm
// 2013-02-23   492   2.1    add Name(), keep log file name; add Dump()
20
// 2013-02-22   491   2.0    add Write(),IsNew(), RlogMsg iface; use lockable
21 10 wfjm
// 2011-01-30   357   1.0    Initial version
22
// ---------------------------------------------------------------------------
23
 
24
/*!
25
  \file
26 29 wfjm
  \version $Id: RlogFile.cpp 631 2015-01-09 21:36:51Z mueller $
27 10 wfjm
  \brief   Implemenation of RlogFile.
28
*/
29
 
30
#include <sys/time.h>
31 29 wfjm
#include <errno.h>
32 10 wfjm
 
33 19 wfjm
#include "boost/thread/locks.hpp"
34
 
35
#include "RosFill.hpp"
36 10 wfjm
#include "RosPrintf.hpp"
37 19 wfjm
#include "RlogMsg.hpp"
38 10 wfjm
 
39 19 wfjm
#include "RlogFile.hpp"
40
 
41 10 wfjm
using namespace std;
42
 
43
/*!
44
  \class Retro::RlogFile
45
  \brief FIXME_docs
46
*/
47
 
48 19 wfjm
// all method definitions in namespace Retro
49
namespace Retro {
50
 
51 10 wfjm
//------------------------------------------+-----------------------------------
52
//! Default constructor
53
 
54
RlogFile::RlogFile()
55 29 wfjm
  : fpExtStream(nullptr),
56 19 wfjm
    fIntStream(),
57
    fNew(true),
58
    fName(),
59
    fMutex()
60 10 wfjm
{
61
  ClearTime();
62
}
63
 
64
//------------------------------------------+-----------------------------------
65
//! FIXME_docs
66
 
67 19 wfjm
RlogFile::RlogFile(std::ostream* os, const std::string& name)
68 10 wfjm
  : fpExtStream(os),
69 19 wfjm
    fIntStream(),
70
    fNew(false),
71 29 wfjm
    fName(BuildinStreamName(os, name)),
72 19 wfjm
    fMutex()
73
{
74
  ClearTime();
75
}
76 10 wfjm
 
77
//------------------------------------------+-----------------------------------
78
//! Destructor
79
 
80
RlogFile::~RlogFile()
81
{}
82
 
83
//------------------------------------------+-----------------------------------
84
//! FIXME_docs
85
 
86 29 wfjm
bool RlogFile::Open(std::string name, RerrMsg& emsg)
87 10 wfjm
{
88 29 wfjm
  std::ostream* os = nullptr;
89
  if      (name == "<cout>" || name == "-") os = &cout;
90
  else if (name == "<cerr>") os = &cerr;
91
  else if (name == "<clog>") os = &clog;
92
  if (os) {
93
    UseStream(os);
94
    return true;
95
  }
96
 
97 19 wfjm
  fNew = false;
98 29 wfjm
  fpExtStream = nullptr;
99 19 wfjm
  fName = name;
100 10 wfjm
  fIntStream.open(name.c_str());
101 29 wfjm
  if (!fIntStream.is_open())
102
    emsg.InitErrno("RlogFile::Open",
103
                   string("open for '") + name + "' failed: ",
104
                   errno);
105 10 wfjm
  return fIntStream.is_open();
106
}
107
 
108
//------------------------------------------+-----------------------------------
109
//! FIXME_docs
110
 
111
void RlogFile::Close()
112
{
113
  fIntStream.close();
114
  return;
115
}
116
 
117
//------------------------------------------+-----------------------------------
118
//! FIXME_docs
119
 
120 19 wfjm
void RlogFile::UseStream(std::ostream* os, const std::string& name)
121 10 wfjm
{
122 19 wfjm
  fNew = false;
123 10 wfjm
  if (fIntStream.is_open()) Close();
124
  fpExtStream = os;
125 29 wfjm
  fName = BuildinStreamName(os, name);
126 10 wfjm
  return;
127
}
128
 
129
//------------------------------------------+-----------------------------------
130
//! FIXME_docs
131
 
132 19 wfjm
void RlogFile::Write(const std::string& str, char tag)
133 10 wfjm
{
134 19 wfjm
  ostream& os = fpExtStream ? *fpExtStream : fIntStream;
135 10 wfjm
 
136 19 wfjm
  boost::lock_guard<RlogFile> lock(*this);
137 10 wfjm
 
138 19 wfjm
  if (tag) {
139
    struct timeval tval;
140 22 wfjm
    ::gettimeofday(&tval, 0);
141 10 wfjm
 
142 19 wfjm
    struct tm tymd;
143 22 wfjm
    ::localtime_r(&tval.tv_sec, &tymd);
144 10 wfjm
 
145 19 wfjm
    if (tymd.tm_year != fTagYear  ||
146
        tymd.tm_mon  != fTagMonth ||
147
        tymd.tm_mday != fTagDay) {
148
 
149
      os << "-+- "
150
         << RosPrintf(tymd.tm_year+1900,"d",4) << "-"
151 22 wfjm
         << RosPrintf(tymd.tm_mon+1,"d0",2) << "-"
152 19 wfjm
         << RosPrintf(tymd.tm_mday,"d0",2) << " -+- \n";
153
 
154
      fTagYear  = tymd.tm_year;
155
      fTagMonth = tymd.tm_mon;
156
      fTagDay   = tymd.tm_mday;
157
    }
158
 
159
    os << "-" << tag << "- "
160
       << RosPrintf(tymd.tm_hour,"d0",2) << ":"
161
       << RosPrintf(tymd.tm_min,"d0",2) << ":"
162
       << RosPrintf(tymd.tm_sec,"d0",2) << "."
163 27 wfjm
       << RosPrintf((int)tval.tv_usec,"d0",6) << " : ";
164 10 wfjm
  }
165
 
166 19 wfjm
  os << str;
167
  if (str[str.length()-1] != '\n') os << endl;
168 10 wfjm
 
169 19 wfjm
  return;
170
}
171 10 wfjm
 
172 19 wfjm
//------------------------------------------+-----------------------------------
173
//! FIXME_docs
174
 
175
void RlogFile::Dump(std::ostream& os, int ind, const char* text) const
176
{
177
  RosFill bl(ind);
178
  os << bl << (text?text:"--") << "RlogFile @ " << this << endl;
179
  os << bl << "  fpExtStream:     " << fpExtStream << endl;
180
  os << bl << "  fIntStream.isopen " << fIntStream.is_open() << endl;
181
  os << bl << "  fNew             " << fNew << endl;
182
  os << bl << "  fName            " << fName << endl;
183
  os << bl << "  fTagYr,Mo,Dy     " << fTagYear << ", " << fTagMonth
184
                                    << ", " << fTagDay << endl;
185
  return;
186 10 wfjm
}
187
 
188
//------------------------------------------+-----------------------------------
189
//! FIXME_docs
190
 
191 19 wfjm
void RlogFile::lock()
192
{
193
  fMutex.lock();
194
  return;
195
}
196
 
197
//------------------------------------------+-----------------------------------
198
//! FIXME_docs
199
 
200
void RlogFile::unlock()
201
{
202
  fMutex.unlock();
203
  return;
204
}
205
 
206
//------------------------------------------+-----------------------------------
207
//! FIXME_docs
208
 
209
RlogFile& RlogFile::operator<<(const RlogMsg& lmsg)
210
{
211
  string str = lmsg.String();
212
  if (str.length() > 0) Write(str, lmsg.Tag());
213
  return *this;
214
}
215
 
216
//------------------------------------------+-----------------------------------
217
//! FIXME_docs
218
 
219 10 wfjm
void RlogFile::ClearTime()
220
{
221
  fTagYear  = -1;
222
  fTagMonth = -1;
223
  fTagDay   = -1;
224
  return;
225
}
226
 
227 29 wfjm
//------------------------------------------+-----------------------------------
228
//! FIXME_docs
229
 
230
  std::string RlogFile::BuildinStreamName(std::ostream* os,
231
                                          const std::string& str)
232
{
233
  if (str.size())  return str;
234
  if (os == &cout) return string("<cout>");
235
  if (os == &cerr) return string("<cerr>");
236
  if (os == &clog) return string("<clog>");
237
  return string("<?stream?>");
238
}
239
 
240 19 wfjm
} // end namespace Retro

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.