SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SGIO.h
Go to the documentation of this file.
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 
61 {
65 };
66 
67 #define NUM_LOG_LEVELS 10
68 #define FBUFSIZE 4096
69 
70 #ifdef DARWIN
71 #include <Availability.h>
72 #ifndef __MAC_10_8
73 #define __MAC_10_8 1080
74 #endif
75 #if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_8
76 #define CONST_DIRENT_T struct dirent
77 #else
78 #define CONST_DIRENT_T const struct dirent
79 #endif // Lion or earlier
80 #else //DARWIN
81 #define CONST_DIRENT_T const struct dirent
82 #endif //DARWIN
83 
84 #define SG_SET_LOCALE_C setlocale(LC_ALL, "C")
85 #define SG_RESET_LOCALE setlocale(LC_ALL, "")
86 
87 #if !defined(SG_UNLIKELY)
88 #if __GNUC__ >= 3
89 #define SG_UNLIKELY(expr) __builtin_expect(expr, 0)
90 #else
91 #define SG_UNLIKELY(expr) expr
92 #endif
93 #endif
94 
95 #ifdef _WIN32
96 #define __PRETTY_FUNCTION__ __FUNCTION__
97 #endif
98 
99 // printf like funktions (with additional severity level)
100 // for object derived from CSGObject
101 #define SG_GCDEBUG(...) { \
102  if (SG_UNLIKELY(io->loglevel_above(MSG_GCDEBUG))) \
103  io->message(MSG_GCDEBUG, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
104 }
105 
106 #define SG_DEBUG(...) { \
107  if (SG_UNLIKELY(io->loglevel_above(MSG_DEBUG))) \
108  io->message(MSG_DEBUG, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
109 }
110 
111 #define SG_OBJ_DEBUG(o,...) { \
112  if (SG_UNLIKELY(o->io->loglevel_above(MSG_DEBUG))) \
113  o->io->message(MSG_DEBUG, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
114 }
115 
116 
117 #define SG_INFO(...) { \
118  if (SG_UNLIKELY(io->loglevel_above(MSG_INFO))) \
119  io->message(MSG_INFO, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
120 }
121 
122 #define SG_CLASS_INFO(c, ...) { \
123  if (SG_UNLIKELY(c::io->loglevel_above(MSG_INFO))) \
124  c::io->message(MSG_INFO, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
125 }
126 
127 #define SG_WARNING(...) { io->message(MSG_WARN, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
128 #define SG_ERROR(...) { io->message(MSG_ERROR, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
129 #define SG_OBJ_ERROR(o, ...) { o->io->message(MSG_ERROR, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
130 #define SG_CLASS_ERROR(c, ...) { c::io->message(MSG_ERROR, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
131 #define SG_UNSTABLE(func, ...) { io->message(MSG_WARN, __PRETTY_FUNCTION__, __FILE__, __LINE__, \
132 __FILE__ ":" func ": Unstable method! Please report if it seems to " \
133 "work or not to the Shogun mailing list. Thanking you in " \
134 "anticipation. " __VA_ARGS__); }
135 
136 #define SG_PRINT(...) { io->message(MSG_MESSAGEONLY, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
137 #define SG_OBJ_PRINT(o, ...) { o->io->message(MSG_MESSAGEONLY, __PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
138 #define SG_NOTIMPLEMENTED { io->not_implemented(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
139 #define SG_DEPRECATED { io->deprecated(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
140 
141 #define SG_PROGRESS(...) { \
142  if (SG_UNLIKELY(io->get_show_progress())) \
143  io->progress(__VA_ARGS__); \
144 }
145 
146 #define SG_OBJ_PROGRESS(o, ...) { \
147  if (SG_UNLIKELY(o->io->get_show_progress()))\
148  o->io->progress(__VA_ARGS__); \
149 }
150 
151 #define SG_ABS_PROGRESS(...) { \
152  if (SG_UNLIKELY(io->get_show_progress())) \
153  io->absolute_progress(__VA_ARGS__); \
154 }
155 
156 #define SG_DONE() { \
157  if (SG_UNLIKELY(io->get_show_progress())) \
158  io->done(); \
159 }
160 
161 // printf like function using the global sg_io object
162 #define SG_SGCDEBUG(...) { \
163  if (SG_UNLIKELY(sg_io->loglevel_above(MSG_GCDEBUG))) \
164  sg_io->message(MSG_GCDEBUG,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__);\
165 }
166 
167 #define SG_SDEBUG(...) { \
168  if (SG_UNLIKELY(sg_io->loglevel_above(MSG_DEBUG))) \
169  sg_io->message(MSG_DEBUG,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
170 }
171 
172 #define SG_SINFO(...) { \
173  if (SG_UNLIKELY(sg_io->loglevel_above(MSG_INFO))) \
174  sg_io->message(MSG_INFO,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
175 }
176 
177 #define SG_SWARNING(...) { sg_io->message(MSG_WARN,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
178 #define SG_SERROR(...) { sg_io->message(MSG_ERROR,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
179 #define SG_SPRINT(...) { sg_io->message(MSG_MESSAGEONLY,__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); }
180 
181 
182 #define SG_SPROGRESS(...) { \
183  if (SG_UNLIKELY(sg_io->get_show_progress())) \
184  sg_io->progress(__VA_ARGS__); \
185 }
186 
187 #define SG_SABS_PROGRESS(...) { \
188  if (SG_UNLIKELY(sg_io->get_show_progress())) \
189  sg_io->absolute_progress(__VA_ARGS__); \
190 }
191 
192 #define SG_SDONE() { \
193  if (SG_UNLIKELY(sg_io->get_show_progress())) \
194  sg_io->done(); \
195 }
196 
197 #define SG_SNOTIMPLEMENTED { sg_io->not_implemented(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
198 #define SG_SDEPRECATED { sg_io->deprecated(__PRETTY_FUNCTION__, __FILE__, __LINE__); }
199 
200 #define ASSERT(x) { \
201  if (SG_UNLIKELY(!(x))) \
202  SG_SERROR("assertion %s failed in %s file %s line %d\n",#x, __PRETTY_FUNCTION__, __FILE__, __LINE__) \
203 }
204 
205 #define REQUIRE(x, ...) { \
206  if (SG_UNLIKELY(!(x))) \
207  SG_SERROR(__VA_ARGS__) \
208 }
209 
210 /* help clang static analyzer to identify custom assertation functions */
211 #ifdef __clang_analyzer__
212 void _clang_fail(void) __attribute__((analyzer_noreturn));
213 
214 #undef SG_ERROR(...)
215 #undef SG_SERROR(...)
216 #define SG_ERROR(...) _clang_fail();
217 #define SG_SERROR(...) _clang_fail();
218 
219 #endif /* __clang_analyzer__ */
220 
228 struct substring
229 {
231  char *start;
233  char *end;
234 };
235 
242 class SGIO
243 {
244  public:
246  SGIO();
248  SGIO(const SGIO& orig);
249 
251  virtual ~SGIO();
252 
257  void set_loglevel(EMessageType level);
258 
263  EMessageType get_loglevel() const;
264 
270  inline bool loglevel_above(EMessageType type) const
271  {
272  return loglevel <= type;
273  }
274 
279  inline bool get_show_progress() const
280  {
281  return show_progress;
282  }
283 
287  {
288  return location_info;
289  }
290 
295  inline bool get_syntax_highlight() const
296  {
297  return syntax_highlight;
298  }
299 
310  void message(EMessageType prio, const char* function, const char* file,
311  int32_t line, const char *fmt, ... ) const;
312 
321  void progress(
322  float64_t current_val,
323  float64_t min_val=0.0, float64_t max_val=1.0, int32_t decimals=1,
324  const char* prefix="PROGRESS:\t");
325 
335  void absolute_progress(
336  float64_t current_val, float64_t val,
337  float64_t min_val=0.0, float64_t max_val=1.0, int32_t decimals=1,
338  const char* prefix="PROGRESS:\t");
339 
344  void done();
345 
347  inline void not_implemented(const char* function, const char* file, int32_t line) const
348  {
349  message(MSG_ERROR, function, file, line, "Sorry, not yet implemented .\n");
350  }
351 
353  inline void deprecated(const char* function, const char* file, int32_t line) const
354  {
355  message(MSG_WARN, function, file, line,
356  "This function is deprecated and will be removed soon.\n");
357  }
358 
364  void buffered_message(EMessageType prio, const char *fmt, ... ) const;
365 
371  static char* skip_spaces(char* str);
372 
378  static char* skip_blanks(char* str);
379 
384  inline FILE* get_target() const
385  {
386  return target;
387  }
388 
393  void set_target(FILE* target);
394 
396  inline void set_target_to_stderr() { set_target(stderr); }
397 
399  inline void set_target_to_stdout() { set_target(stdout); }
400 
402  inline void enable_progress()
403  {
404  show_progress=true;
405 
406  // static functions like CSVM::classify_example_helper call SG_PROGRESS
407  if (sg_io!=this)
409  }
410 
412  inline void disable_progress()
413  {
414  show_progress=false;
415 
416  // static functions like CSVM::classify_example_helper call SG_PROGRESS
417  if (sg_io!=this)
419  }
420 
426  inline void set_location_info(EMessageLocation location)
427  {
428  location_info = location;
429 
430  if (sg_io!=this)
431  sg_io->set_location_info(location);
432  }
433 
436  {
437  syntax_highlight=true;
438 
439  if (sg_io!=this)
441  }
442 
445  {
446  syntax_highlight=false;
447 
448  if (sg_io!=this)
450  }
451 
456  static inline void set_dirname(const char* dirname)
457  {
458  strncpy(directory_name, dirname, FBUFSIZE);
459  }
460 
467  static char* concat_filename(const char* filename);
468 
474  static int filter(CONST_DIRENT_T* d);
475 
481  static char* c_string_of_substring(substring s);
482 
487  static void print_substring(substring s);
488 
496 
503 
509  static int32_t int_of_substring(substring s);
510 
516  static uint32_t ulong_of_substring(substring s);
517 
523  static uint32_t ss_length(substring s);
524 
529  int32_t ref();
530 
535  int32_t ref_count() const;
536 
542  int32_t unref();
543 
545  inline const char* get_name() { return "SGIO"; }
546 
547  protected:
554  const char* get_msg_intro(EMessageType prio) const;
555 
556  protected:
558  FILE* target;
572 
580  static const char* message_strings[NUM_LOG_LEVELS];
581 
583  static char file_buffer[FBUFSIZE];
585  static char directory_name[FBUFSIZE];
586 
587  private:
588  RefCount* m_refcount;
589 };
590 }
591 #endif // __SGIO_H__

SHOGUN Machine Learning Toolbox - Documentation