TRF Language Model
wb-log.cpp
Go to the documentation of this file.
1 // You may obtain a copy of the License at
2 //
3 // http://www.apache.org/licenses/LICENSE-2.0
4 //
5 // Unless required by applicable law or agreed to in writing, software
6 // distributed under the License is distributed on an "AS IS" BASIS,
7 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8 // See the License for the specific language governing permissions and
9 // limitations under the License.
10 //
11 // Copyright 2014-2015 Tsinghua University
12 // Author: wb.th08@gmail.com (Bin Wang), ozj@tsinghua.edu.cn (Zhijian Ou)
13 //
14 // All h, cpp, cc, and script files (e.g. bat, sh, pl, py) should include the above
15 // license declaration. Different coding language may use different comment styles.
16 
17 
18 #include "wb-log.h"
19 
20 namespace wb
21 {
23 
25  {
26  m_bOutputCmd = true;
27  m_bOutputLog = true;
28  m_nLevel = 0;
29 
30 #ifndef __linux
31  char strFileName[255];
32  strcpy(strFileName, strrchr(__argv[0], '\\') + 1);
33  char *p = strstr(strFileName, ".");
34  if (p)
35  strcpy(p, ".log");
36  else
37  strcat(strFileName, ".log");
38 
39  m_fileLog.open(strFileName, ios_base::app);
40  if (!m_fileLog.good())
41  {
42  cout << "Open Log file faled! [" << strFileName << "]" << endl;
43  exit(0);
44  }
45 #endif
46 
47 
48  char strTime[128];
49  char strDate[128];
50  _strdate(strDate);
51  _strtime(strTime);
52  m_fileLog << endl;
53  *this << "[Begin]\t" << strDate << " " << strTime << endl;
54 
55  }
57  {
58  char strTime[128];
59  char strDate[128];
60  _strdate(strDate);
61  _strtime(strTime);
62  m_bOutputLog = true;
63  m_bOutputCmd = true;
64  *this << "[End]\t" << strDate << " " << strTime << endl;
65  }
66 
68  void Log::ReFile(const char *path, bool bNew/* = true*/)
69  {
70  m_bOutputCmd = true;
71  m_bOutputLog = true;
72  m_nLevel = 0;
73 
74  m_fileLog.close();
75  if (bNew)
76  m_fileLog.open(path);
77  else
78  m_fileLog.open(path, ios_base::app);
79 
80  if (!m_fileLog.good())
81  {
82  cout << "Open Log file faled! [" << path << "]" << endl;
83  exit(0);
84  }
85 
86  char strTime[128];
87  char strDate[128];
88  _strdate(strDate);
89  _strtime(strTime);
90  m_fileLog << endl;
91  *this << "[ReFile] bNew = " << bNew << endl;
92  *this << "[Begin]\t" << strDate << " " << strTime << endl;
93  }
94 
96  bool &Log::bOutputCmd() { return m_bOutputCmd; }
98  bool &Log::bOutputLog() { return m_bOutputLog; }
99 
100  Log &Log::operator << (ostream& (*op) (ostream&)) { wbLog_Output(*op) }
104  Log &Log::operator << (long long x) { wbLog_Output(x) }
105  Log &Log::operator << (unsigned int x) { wbLog_Output(x) }
106  Log &Log::operator << (unsigned short x) { wbLog_Output(x) }
107  Log &Log::operator << (unsigned long x) { wbLog_Output(x) }
111  Log &Log::operator << (const char* x) {
112  //�ж�ָ���Ƿ�Ϸ�
113  if (x) {
114  wbLog_Output(x)
115  }
116  else {
117  wbLog_Output("[NULL]")
118  }
119  }
120  Log &Log::operator << (const void* x) {
121  //�ж�ָ���Ƿ�Ϸ�
122  if (x) {
123  wbLog_Output(x)
124  }
125  else {
126  wbLog_Output("[NULL]")
127  }
128  }
130  //��bool����ת�����ַ�����ӡ���
131  if (x) {
132  wbLog_Output("true")
133  }
134  else {
135  wbLog_Output("false")
136  }
137  }
138  Log &Log::operator << (string &x) {
139  wbLog_Output(x.c_str());
140  }
142  void Log::LevelDown() { m_nLevel++; }
144  void Log::LevelUp() { m_nLevel = (m_nLevel <= 0) ? 0 : m_nLevel - 1; }
145 
146  void Log::Progress(long long n /* = -1 */, bool bInit /* = false */, long long total /* = 100 */, const char* head /* = "" */)
147  {
148  if (bInit) {
149  m_bar.Reset(n, total, head, "[>]");
150  }
151  else {
152  m_bar.Update(n);
153  }
154  }
155  void Log::Progress(FILE *fp, bool bInit /* = false */, const char* head/* ="" */)
156  {
157  if (bInit)
158  {
159  long long nCur = _ftelli64(fp);
160  _fseeki64(fp, 0, SEEK_END);
161  Progress(nCur, true, _ftelli64(fp), head);
162  _fseeki64(fp, nCur, SEEK_SET);
163  }
164  else
165  {
166  Progress(_ftelli64(fp));
167  }
168  }
169 
170 
171  void ProgressBar::Reset(long long total /* = 100 */, const char *head /* = "" */, const char* sym /* = "[/* =]" */)
172  {
173  m_total = total;
174  m_num = 0;
175  memcpy(m_symbol, sym, sizeof(char) * 3);
176  m_lastprec = -1;
177  m_strhead = head;
178  Update(0);
179  }
180  void ProgressBar::Reset(long long n, long long total /* = 100 */, const char *head /* = "" */, const char* sym /* = "[/* =]" */)
181  {
182  m_total = total;
183  m_num = 0;
184  memcpy(m_symbol, sym, sizeof(char) * 3);
185  m_lastprec = -1;
186  m_strhead = head;
187  Update(n);
188  }
189  void ProgressBar::Update(long long n)
190  {
191  if (n < 0) {
192  n = m_num;
193  m_num++;
194  }
195  else {
196  m_num = n;
197  }
198  int curprec = 100 * n / m_total;
199  if (curprec > m_lastprec) {
200  cout << "\r" << m_strhead.c_str() << " ";
201  int barlen = (int)(1.0 * curprec / 100 * m_barmaxlen);
202  cout << m_symbol[0];
203  for (int i = 0; i < barlen; i++)
204  cout << m_symbol[1];
205  for (int i = 0; i < m_barmaxlen - barlen; i++)
206  cout << " ";
207  cout << m_symbol[2]<<" ";
208  cout << setprecision(2) << setiosflags(ios::fixed) << 1.0 * n / m_total * 100 << "%" << setprecision(6);
209  cout.flush();
210 
211  m_lastprec = curprec;
212  if (curprec == 100) {
213  cout << endl;
214  }
215  }
216  }
217 }
this class can output to the cmd window and log files simultaneously. In wb-log.cpp, there are a Log variable "lout", which can be directly used just like "cout". For example:
Definition: wb-log.h:110
void ReFile(const char *path, bool bNew=true)
relocate the log file. The defualt log file is "program name".log
Definition: wb-log.cpp:68
bool & bOutputCmd()
if output to the cmd window
Definition: wb-log.cpp:96
short m_nLevel
output level
Definition: wb-log.h:117
void LevelDown()
level down
Definition: wb-log.cpp:142
void Reset(long long total=100, const char *head="", const char *sym="[=]")
reset the progress bar
Definition: wb-log.cpp:171
ofstream m_fileLog
log file stream
Definition: wb-log.h:113
Log & operator<<(ostream &(*op)(ostream &))
output.
Definition: wb-log.cpp:100
void LevelUp()
level up
Definition: wb-log.cpp:144
a definition of a class Log, which can output to the cmd window and the log file simultaneously. In wb-log.cpp, there are a Log variable "lout", which can be directly used just like "cout". For example:
bool bInit
#define wbLog_Output(x)
Definition: wb-log.h:58
bool m_bOutputCmd
if output to the cmd window
Definition: wb-log.h:115
bool m_bOutputLog
if output to the log file
Definition: wb-log.h:114
void Progress(long long n=-1, bool bInit=false, long long total=100, const char *head="")
progress bar
Definition: wb-log.cpp:146
bool & bOutputLog()
if output to the log file
Definition: wb-log.cpp:98
Log lout
the defination is in wb-log.cpp
Definition: wb-log.cpp:22
~Log()
destructor
Definition: wb-log.cpp:56
void Update(long long n=-1)
update the progress bar. n should be from 1 to m_total
Definition: wb-log.cpp:189
Log()
constructor
Definition: wb-log.cpp:24
ProgressBar m_bar
the build-in progerss bar.
Definition: wb-log.h:119
define all the code written by Bin Wang.
Definition: wb-file.cpp:21