TRF Language Model
wb-log.h
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 
36 #pragma once
37 #include <iostream>
38 #include <fstream>
39 #include <iomanip>
40 #include <ctime>
41 #include <cstring>
42 using namespace std;
43 #include "wb-linux.h"
44 
48 namespace wb
49 {
54 #define wbLog_LevelSpace "\t"
55 
56 #define wbLog_OutputSpace(io) \
57  for (int i=0; i<m_nLevel; i++) io<<wbLog_LevelSpace;
58 #define wbLog_Output(x) \
59  if (m_bOutputLog) { {wbLog_OutputSpace(m_fileLog)} m_fileLog<<x; } \
60  if (m_bOutputCmd) { {wbLog_OutputSpace(cout)} cout<<x; }\
61  return *this;
62 
65  {
66  private:
67  long long m_num;
68  long long m_total;
69  char m_symbol[3];
70  int m_lastprec;
71  const int m_barmaxlen = 50;
72 
73  string m_strhead;
74  public:
76  m_total = 100;
77  memcpy(m_symbol, "[=]", sizeof(3));
78  m_lastprec = -1;
79  m_strhead = "";
80  }
81  ProgressBar(long long total, const char *head = "", const char* sym = "[=]"){
82  Reset(total, head, sym);
83  }
85  void Reset(long long total = 100, const char *head = "", const char* sym = "[=]");
87  void Reset(long long n, long long total = 100, const char *head = "", const char* sym = "[=]");
89  void Update(long long n = -1);
90  };
91 
110  class Log
111  {
112  protected:
113  ofstream m_fileLog;
116 
117  short m_nLevel;
118 
120  public:
122  Log();
124  ~Log();
126 
129  void ReFile(const char *path, bool bNew = true);
131  bool &bOutputCmd();
133  bool &bOutputLog();
135  Log &operator << (ostream& (*op) (ostream&));
137  //template <typename _Arg>
138  //Log &operator << (const _Smanip<_Arg>& _Manip) { cout << _Manip; return *this; }
139  Log &operator << (int x);
140  Log &operator << (short x);
141  Log &operator << (long x);
142  Log &operator << (long long x);
143  Log &operator << (unsigned int x);
144  Log &operator << (unsigned short x);
145  Log &operator << (unsigned long x);
146  Log &operator << (float x);
147  Log &operator << (double x);
148  Log &operator << (char x);
149  Log &operator << (const char* x);
150  Log &operator << (const void* x);
151  Log &operator << (bool x);
152  Log &operator << (string &x);
154  template <typename T>
156  Log &output(T *pArray, int n, const char *pgap=" ");
158  void LevelDown();
160  void LevelUp();
162  void Progress(long long n = -1, bool bInit = false, long long total = 100, const char* head = "");
164  void Progress(FILE *fp, bool bInit = false, const char* head="");
165  };
166 
167  extern Log lout;
168 
169  template <typename T>
170  Log &Log::output(T *pArray, int n, const char *pgap/*=" "*/)
171  {
172  for (int i = 0; i < n; i++) {
173  *this << pArray[i] << pgap;
174  }
175  return *this;
176  }
177 
178 
179 #define lout_variable(x) {wb::lout<<#x" = "<<x<<endl;}
180 #define lout_variable_precent(x,y) {wb::lout<<#x" = "<<x<<" ("<<100.0*(x)/(y)<<"%) /"#y<<"="<<y<<endl;}
181 #define lout_variable_rate(x,y) {wb::lout<<100.0*(x)/(y)<<"%("<<x<<"/"<<y<<")";}
182 #define lout_array(x, n) {wb::lout<<#x"=[ "; for(int i=0; i<n; i++) wb::lout<<x[i]<<" "; wb::lout<<"]"<<endl; }
183 #define lout_error(x) {wb::lout.bOutputCmd()=true; wb::lout<<"[ERROR] "<<x<<endl; exit(0);}
184 #define lout_warning(x) {wb::lout<<"[WARNING] "<<x<<endl;}
185 #define lout_assert(p) {if(!(p)) lout_error("! ("#p")"); }
186 
187 #define precent(x, n) (x)<<"("<<100.0*(x)/(n)<<"%)"
188 
189 
190 
191 
192 
195 }
196 
197 
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
short m_nLevel
output level
Definition: wb-log.h:117
ofstream m_fileLog
log file stream
Definition: wb-log.h:113
a progress bar class
Definition: wb-log.h:64
bool bInit
lout Progress(0, true, nCorpusNum - 1, "omp GetLL")
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
Log lout
the defination is in wb-log.cpp
Definition: wb-log.cpp:22
ProgressBar(long long total, const char *head="", const char *sym="[=]")
Definition: wb-log.h:81
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