SHOGUN  v3.2.0
SGIO.h
浏览该文件的文档.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2013 Soeren Sonnenburg
8  * Written (W) 1999-2008 Gunnar Raetsch
9  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10  * Copyright (C) 2010-2013 Soeren Sonnenburg
11  */
12 
13 #ifndef __SGIO_H__
14 #define __SGIO_H__
15 
16 #include <stdio.h>
17 #include <stdarg.h>
18 #include <string.h>
19 #include <dirent.h>
20 #ifndef _WIN32
21 #include <unistd.h>
22 #endif
23 #include <locale.h>
24 
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 
28 #include <shogun/lib/common.h>
29 #include <shogun/base/init.h>
30 
31 namespace shogun
32 {
33  class RefCount;
34  class SGIO;
36  extern SGIO* sg_io;
37 }
38 
39 
40 namespace shogun
41 {
47 {
58 };
59 
64 {
68 };
69 
70 #define NUM_LOG_LEVELS 10
71 #define FBUFSIZE 4096
72 
73 #ifdef DARWIN
74 #include <Availability.h>
75 #ifndef __MAC_10_8
76 #define __MAC_10_8 1080
77 #endif
78 #if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_8
79 #define CONST_DIRENT_T struct dirent
80 #else
81 #define CONST_DIRENT_T const struct dirent
82 #endif // Lion or earlier
83 #else //DARWIN
84 #define CONST_DIRENT_T const struct dirent
85 #endif //DARWIN
86 
87 #define SG_SET_LOCALE_C setlocale(LC_ALL, "C")
88 #define SG_RESET_LOCALE setlocale(LC_ALL, "")
89 
90 #if !defined(SG_UNLIKELY)
91 #if __GNUC__ >= 3
92 #define SG_UNLIKELY(expr) __builtin_expect(expr, 0)
93 #else
94 #define SG_UNLIKELY(expr) expr
95 #endif
96 #endif
97 
98 #ifdef _WIN32
99 #define __PRETTY_FUNCTION__ __FUNCTION__
100 #endif
101 
102 // printf like funktions (with additional severity level)
103 // for object derived from CSGObject
104 #define SG_GCDEBUG(...) { \
105  if (SG_UNLIKELY(io->loglevel_above(MSG_GCDEBUG))) \
106  io->message(MSG_GCDEBUG, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
107 }
108 
109 #define SG_DEBUG(...) { \
110  if (SG_UNLIKELY(io->loglevel_above(MSG_DEBUG))) \
111  io->message(MSG_DEBUG, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
112 }
113 
114 #define SG_OBJ_DEBUG(o,...) { \
115  if (SG_UNLIKELY(o->io->loglevel_above(MSG_DEBUG))) \
116  o->io->message(MSG_DEBUG, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
117 }
118 
119 
120 #define SG_INFO(...) { \
121  if (SG_UNLIKELY(io->loglevel_above(MSG_INFO))) \
122  io->message(MSG_INFO, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
123 }
124 
125 #define SG_CLASS_INFO(c, ...) { \
126  if (SG_UNLIKELY(c::io->loglevel_above(MSG_INFO))) \
127  c::io->message(MSG_INFO, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
128 }
129 
130 #define SG_WARNING(...) { io->message(MSG_WARN, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
131 #define SG_ERROR(...) { io->message(MSG_ERROR, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
132 #define SG_OBJ_ERROR(o, ...) { o->io->message(MSG_ERROR, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
133 #define SG_CLASS_ERROR(c, ...) { c::io->message(MSG_ERROR, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
134 #define SG_UNSTABLE(func, ...) { io->message(MSG_WARN, __PRETTY_FUNCTION__, __FILE__, __LINE__, \
135 __FILE__ ":" func ": Unstable method! Please report if it seems to " \
136 "work or not to the Shogun mailing list. Thanking you in " \
137 "anticipation. " __VA_ARGS__); }
138 
139 #define SG_PRINT(...) { io->message(MSG_MESSAGEONLY, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
140 #define SG_OBJ_PRINT(o, ...) { o->io->message(MSG_MESSAGEONLY, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
141 #define SG_NOTIMPLEMENTED { io->not_implemented(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
142 #define SG_DEPRECATED { io->deprecated(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
143 
144 #define SG_PROGRESS(...) { \
145  if (SG_UNLIKELY(io->get_show_progress())) \
146  io->progress(__VA_ARGS__); \
147 }
148 
149 #define SG_OBJ_PROGRESS(o, ...) { \
150  if (SG_UNLIKELY(o->io->get_show_progress()))\
151  o->io->progress(__VA_ARGS__); \
152 }
153 
154 #define SG_ABS_PROGRESS(...) { \
155  if (SG_UNLIKELY(io->get_show_progress())) \
156  io->absolute_progress(__VA_ARGS__); \
157 }
158 
159 #define SG_DONE() { \
160  if (SG_UNLIKELY(io->get_show_progress())) \
161  io->done(); \
162 }
163 
164 // printf like function using the global sg_io object
165 #define SG_SGCDEBUG(...) { \
166  if (SG_UNLIKELY(sg_io->loglevel_above(MSG_GCDEBUG))) \
167  sg_io->message(MSG_GCDEBUG,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__);\
168 }
169 
170 #define SG_SDEBUG(...) { \
171  if (SG_UNLIKELY(sg_io->loglevel_above(MSG_DEBUG))) \
172  sg_io->message(MSG_DEBUG,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
173 }
174 
175 #define SG_SINFO(...) { \
176  if (SG_UNLIKELY(sg_io->loglevel_above(MSG_INFO))) \
177  sg_io->message(MSG_INFO,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
178 }
179 
180 #define SG_SWARNING(...) { sg_io->message(MSG_WARN,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
181 #define SG_SERROR(...) { sg_io->message(MSG_ERROR,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
182 #define SG_SPRINT(...) { sg_io->message(MSG_MESSAGEONLY,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
183 
184 
185 #define SG_SPROGRESS(...) { \
186  if (SG_UNLIKELY(sg_io->get_show_progress())) \
187  sg_io->progress(__VA_ARGS__); \
188 }
189 
190 #define SG_SABS_PROGRESS(...) { \
191  if (SG_UNLIKELY(sg_io->get_show_progress())) \
192  sg_io->absolute_progress(__VA_ARGS__); \
193 }
194 
195 #define SG_SDONE() { \
196  if (SG_UNLIKELY(sg_io->get_show_progress())) \
197  sg_io->done(); \
198 }
199 
200 #define SG_SNOTIMPLEMENTED { sg_io->not_implemented(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
201 #define SG_SDEPRECATED { sg_io->deprecated(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
202 
203 #define ASSERT(x) { \
204  if (SG_UNLIKELY(!(x))) \
205  SG_SERROR("assertion %s failed in %s file %s line %d\n",#x, __PRETTY_FUNCTION__, __FILE__, __LINE__) \
206 }
207 
208 #define REQUIRE(x, ...) { \
209  if (SG_UNLIKELY(!(x))) \
210  SG_SERROR(__VA_ARGS__) \
211 }
212 
213 /* help clang static analyzer to identify custom assertation functions */
214 #ifdef __clang_analyzer__
215 void _clang_fail(void) __attribute__((analyzer_noreturn));
216 
217 #undef SG_ERROR(...)
218 #undef SG_SERROR(...)
219 #define SG_ERROR(...) _clang_fail();
220 #define SG_SERROR(...) _clang_fail();
221 
222 #endif /* __clang_analyzer__ */
223 
231 struct substring
232 {
234  char *start;
236  char *end;
237 };
238 
245 class SGIO
246 {
247  public:
249  SGIO();
251  SGIO(const SGIO& orig);
252 
254  virtual ~SGIO();
255 
260  void set_loglevel(EMessageType level);
261 
266  EMessageType get_loglevel() const;
267 
273  inline bool loglevel_above(EMessageType type) const
274  {
275  return loglevel <= type;
276  }
277 
282  inline bool get_show_progress() const
283  {
284  return show_progress;
285  }
286 
290  {
291  return location_info;
292  }
293 
298  inline bool get_syntax_highlight() const
299  {
300  return syntax_highlight;
301  }
302 
314  void message(EMessageType prio, const char* function, const char* file,
315  int32_t line, const char *fmt, ... ) const;
316 
325  void progress(
326  float64_t current_val,
327  float64_t min_val=0.0, float64_t max_val=1.0, int32_t decimals=1,
328  const char* prefix="PROGRESS:\t");
329 
339  void absolute_progress(
340  float64_t current_val, float64_t val,
341  float64_t min_val=0.0, float64_t max_val=1.0, int32_t decimals=1,
342  const char* prefix="PROGRESS:\t");
343 
348  void done();
349 
351  inline void not_implemented(const char* function, const char* file, int32_t line) const
352  {
353  message(MSG_ERROR, function, file, line, "Sorry, not yet implemented .\n");
354  }
355 
357  inline void deprecated(const char* function, const char* file, int32_t line) const
358  {
359  message(MSG_WARN, function, file, line,
360  "This function is deprecated and will be removed soon.\n");
361  }
362 
368  void buffered_message(EMessageType prio, const char *fmt, ... ) const;
369 
375  static char* skip_spaces(char* str);
376 
382  static char* skip_blanks(char* str);
383 
388  inline FILE* get_target() const
389  {
390  return target;
391  }
392 
397  void set_target(FILE* target);
398 
400  inline void set_target_to_stderr() { set_target(stderr); }
401 
403  inline void set_target_to_stdout() { set_target(stdout); }
404 
406  inline void enable_progress()
407  {
408  show_progress=true;
409 
410  // static functions like CSVM::classify_example_helper call SG_PROGRESS
411  if (sg_io!=this)
413  }
414 
416  inline void disable_progress()
417  {
418  show_progress=false;
419 
420  // static functions like CSVM::classify_example_helper call SG_PROGRESS
421  if (sg_io!=this)
423  }
424 
430  inline void set_location_info(EMessageLocation location)
431  {
432  location_info = location;
433 
434  if (sg_io!=this)
435  sg_io->set_location_info(location);
436  }
437 
440  {
441  syntax_highlight=true;
442 
443  if (sg_io!=this)
445  }
446 
449  {
450  syntax_highlight=false;
451 
452  if (sg_io!=this)
454  }
455 
460  static inline void set_dirname(const char* dirname)
461  {
462  strncpy(directory_name, dirname, FBUFSIZE);
463  }
464 
471  static char* concat_filename(const char* filename);
472 
478  static int filter(CONST_DIRENT_T* d);
479 
485  static char* c_string_of_substring(substring s);
486 
491  static void print_substring(substring s);
492 
499  static float32_t float_of_substring(substring s);
500 
506  static float64_t double_of_substring(substring s);
507 
513  static int32_t int_of_substring(substring s);
514 
520  static uint32_t ulong_of_substring(substring s);
521 
527  static uint32_t ss_length(substring s);
528 
533  int32_t ref();
534 
539  int32_t ref_count() const;
540 
546  int32_t unref();
547 
549  inline const char* get_name() { return "SGIO"; }
550 
551  protected:
558  const char* get_msg_intro(EMessageType prio) const;
559 
560  protected:
562  FILE* target;
576 
580  static const EMessageType levels[NUM_LOG_LEVELS];
582  static const char* message_strings_highlighted[NUM_LOG_LEVELS];
584  static const char* message_strings[NUM_LOG_LEVELS];
585 
587  static char file_buffer[FBUFSIZE];
589  static char directory_name[FBUFSIZE];
590 
591  private:
592  RefCount* m_refcount;
593 };
594 }
595 #endif // __SGIO_H__
bool loglevel_above(EMessageType type) const
Definition: SGIO.h:273
bool get_show_progress() const
Definition: SGIO.h:282
char * end
Definition: SGIO.h:236
#define NUM_LOG_LEVELS
Definition: SGIO.h:70
float64_t last_progress
Definition: SGIO.h:568
char * start
Definition: SGIO.h:234
void deprecated(const char *function, const char *file, int32_t line) const
Definition: SGIO.h:357
struct Substring, specified by start position and end position.
Definition: SGIO.h:231
void disable_syntax_highlighting()
Definition: SGIO.h:448
#define FBUFSIZE
Definition: SGIO.h:71
SGIO * sg_io
Definition: init.cpp:26
bool get_syntax_highlight() const
Definition: SGIO.h:298
FILE * target
Definition: SGIO.h:562
void disable_progress()
Definition: SGIO.h:416
void enable_syntax_highlighting()
Definition: SGIO.h:439
float64_t last_progress_time
Definition: SGIO.h:564
double float64_t
Definition: common.h:48
void set_target_to_stdout()
Definition: SGIO.h:403
#define CONST_DIRENT_T
Definition: SGIO.h:84
float64_t progress_start_time
Definition: SGIO.h:566
float float32_t
Definition: common.h:47
EMessageType
Definition: SGIO.h:46
EMessageLocation location_info
Definition: SGIO.h:573
static void set_dirname(const char *dirname)
Definition: SGIO.h:460
EMessageType loglevel
Definition: SGIO.h:578
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:16
void not_implemented(const char *function, const char *file, int32_t line) const
Definition: SGIO.h:351
const char * get_name()
Definition: SGIO.h:549
EMessageLocation get_location_info() const
Definition: SGIO.h:289
EMessageLocation
Definition: SGIO.h:63
Class SGIO, used to do input output operations throughout shogun.
Definition: SGIO.h:245
bool show_progress
Definition: SGIO.h:570
bool syntax_highlight
Definition: SGIO.h:575
void set_location_info(EMessageLocation location)
Definition: SGIO.h:430
void set_target_to_stderr()
Definition: SGIO.h:400
FILE * get_target() const
Definition: SGIO.h:388
void enable_progress()
Definition: SGIO.h:406

SHOGUN Machine Learning Toolbox - Documentation