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 19

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

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