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

Subversion Repositories w11

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 22 wfjm
// $Id: RlogFile.cpp 539 2013-10-13 17:06:35Z mueller $
2 10 wfjm
//
3 19 wfjm
// Copyright 2011-2013 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 22 wfjm
// 2013-10-11   539   2.1.1  fix date print (month was off by one)
17 19 wfjm
// 2013-02-23   492   2.1    add Name(), keep log file name; add Dump()
18
// 2013-02-22   491   2.0    add Write(),IsNew(), RlogMsg iface; use lockable
19 10 wfjm
// 2011-01-30   357   1.0    Initial version
20
// ---------------------------------------------------------------------------
21
 
22
/*!
23
  \file
24 22 wfjm
  \version $Id: RlogFile.cpp 539 2013-10-13 17:06:35Z mueller $
25 10 wfjm
  \brief   Implemenation of RlogFile.
26
*/
27
 
28
#include <sys/time.h>
29
 
30 19 wfjm
#include "boost/thread/locks.hpp"
31
 
32
#include "RosFill.hpp"
33 10 wfjm
#include "RosPrintf.hpp"
34 19 wfjm
#include "RlogMsg.hpp"
35 10 wfjm
 
36 19 wfjm
#include "RlogFile.hpp"
37
 
38 10 wfjm
using namespace std;
39
 
40
/*!
41
  \class Retro::RlogFile
42
  \brief FIXME_docs
43
*/
44
 
45 19 wfjm
// all method definitions in namespace Retro
46
namespace Retro {
47
 
48 10 wfjm
//------------------------------------------+-----------------------------------
49
//! Default constructor
50
 
51
RlogFile::RlogFile()
52
  : fpExtStream(0),
53 19 wfjm
    fIntStream(),
54
    fNew(true),
55
    fName(),
56
    fMutex()
57 10 wfjm
{
58
  ClearTime();
59
}
60
 
61
//------------------------------------------+-----------------------------------
62
//! FIXME_docs
63
 
64 19 wfjm
RlogFile::RlogFile(std::ostream* os, const std::string& name)
65 10 wfjm
  : fpExtStream(os),
66 19 wfjm
    fIntStream(),
67
    fNew(false),
68
    fName(name),
69
    fMutex()
70
{
71
  ClearTime();
72
}
73 10 wfjm
 
74
//------------------------------------------+-----------------------------------
75
//! Destructor
76
 
77
RlogFile::~RlogFile()
78
{}
79
 
80
//------------------------------------------+-----------------------------------
81
//! FIXME_docs
82
 
83
bool RlogFile::Open(std::string name)
84
{
85 19 wfjm
  fNew = false;
86 10 wfjm
  fpExtStream = 0;
87 19 wfjm
  fName = name;
88 10 wfjm
  fIntStream.open(name.c_str());
89
  return fIntStream.is_open();
90
}
91
 
92
//------------------------------------------+-----------------------------------
93
//! FIXME_docs
94
 
95
void RlogFile::Close()
96
{
97
  fIntStream.close();
98
  return;
99
}
100
 
101
//------------------------------------------+-----------------------------------
102
//! FIXME_docs
103
 
104 19 wfjm
void RlogFile::UseStream(std::ostream* os, const std::string& name)
105 10 wfjm
{
106 19 wfjm
  fNew = false;
107 10 wfjm
  if (fIntStream.is_open()) Close();
108
  fpExtStream = os;
109 19 wfjm
  fName = name;
110 10 wfjm
  return;
111
}
112
 
113
//------------------------------------------+-----------------------------------
114
//! FIXME_docs
115
 
116 19 wfjm
void RlogFile::Write(const std::string& str, char tag)
117 10 wfjm
{
118 19 wfjm
  ostream& os = fpExtStream ? *fpExtStream : fIntStream;
119 10 wfjm
 
120 19 wfjm
  boost::lock_guard<RlogFile> lock(*this);
121 10 wfjm
 
122 19 wfjm
  if (tag) {
123
    struct timeval tval;
124 22 wfjm
    ::gettimeofday(&tval, 0);
125 10 wfjm
 
126 19 wfjm
    struct tm tymd;
127 22 wfjm
    ::localtime_r(&tval.tv_sec, &tymd);
128 10 wfjm
 
129 19 wfjm
    if (tymd.tm_year != fTagYear  ||
130
        tymd.tm_mon  != fTagMonth ||
131
        tymd.tm_mday != fTagDay) {
132
 
133
      os << "-+- "
134
         << RosPrintf(tymd.tm_year+1900,"d",4) << "-"
135 22 wfjm
         << RosPrintf(tymd.tm_mon+1,"d0",2) << "-"
136 19 wfjm
         << RosPrintf(tymd.tm_mday,"d0",2) << " -+- \n";
137
 
138
      fTagYear  = tymd.tm_year;
139
      fTagMonth = tymd.tm_mon;
140
      fTagDay   = tymd.tm_mday;
141
    }
142
 
143
    int usec = (int)(tval.tv_usec/1000);
144
 
145
    os << "-" << tag << "- "
146
       << RosPrintf(tymd.tm_hour,"d0",2) << ":"
147
       << RosPrintf(tymd.tm_min,"d0",2) << ":"
148
       << RosPrintf(tymd.tm_sec,"d0",2) << "."
149
       << RosPrintf(usec,"d0",3) << " : ";
150 10 wfjm
  }
151
 
152 19 wfjm
  os << str;
153
  if (str[str.length()-1] != '\n') os << endl;
154 10 wfjm
 
155 19 wfjm
  return;
156
}
157 10 wfjm
 
158 19 wfjm
//------------------------------------------+-----------------------------------
159
//! FIXME_docs
160
 
161
void RlogFile::Dump(std::ostream& os, int ind, const char* text) const
162
{
163
  RosFill bl(ind);
164
  os << bl << (text?text:"--") << "RlogFile @ " << this << endl;
165
  os << bl << "  fpExtStream:     " << fpExtStream << endl;
166
  os << bl << "  fIntStream.isopen " << fIntStream.is_open() << endl;
167
  os << bl << "  fNew             " << fNew << endl;
168
  os << bl << "  fName            " << fName << endl;
169
  os << bl << "  fTagYr,Mo,Dy     " << fTagYear << ", " << fTagMonth
170
                                    << ", " << fTagDay << endl;
171
  return;
172 10 wfjm
}
173
 
174
//------------------------------------------+-----------------------------------
175
//! FIXME_docs
176
 
177 19 wfjm
void RlogFile::lock()
178
{
179
  fMutex.lock();
180
  return;
181
}
182
 
183
//------------------------------------------+-----------------------------------
184
//! FIXME_docs
185
 
186
void RlogFile::unlock()
187
{
188
  fMutex.unlock();
189
  return;
190
}
191
 
192
//------------------------------------------+-----------------------------------
193
//! FIXME_docs
194
 
195
RlogFile& RlogFile::operator<<(const RlogMsg& lmsg)
196
{
197
  string str = lmsg.String();
198
  if (str.length() > 0) Write(str, lmsg.Tag());
199
  return *this;
200
}
201
 
202
//------------------------------------------+-----------------------------------
203
//! FIXME_docs
204
 
205 10 wfjm
void RlogFile::ClearTime()
206
{
207
  fTagYear  = -1;
208
  fTagMonth = -1;
209
  fTagDay   = -1;
210
  return;
211
}
212
 
213 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.