SHOGUN  v2.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-2009 Soeren Sonnenburg
8  * Written (W) 1999-2008 Gunnar Raetsch
9  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10  */
11 
12 #ifndef __SGIO_H__
13 #define __SGIO_H__
14 
15 #include <stdio.h>
16 #include <stdarg.h>
17 #include <string.h>
18 #include <dirent.h>
19 #include <unistd.h>
20 #include <locale.h>
21 
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 
25 #include <shogun/lib/common.h>
26 #include <shogun/base/init.h>
27 
28 namespace shogun
29 {
30  class SGIO;
32  extern SGIO* sg_io;
33 }
34 
35 
36 namespace shogun
37 {
43 {
54 };
55 
56 
57 #define NUM_LOG_LEVELS 10
58 #define FBUFSIZE 4096
59 
60 #ifdef DARWIN
61 #include <Availability.h>
62 #if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_8
63 #define CONST_DIRENT_T struct dirent
64 #else
65 #define CONST_DIRENT_T const struct dirent
66 #endif // Mountain Lion or earlier
67 #else //DARWIN
68 #define CONST_DIRENT_T const struct dirent
69 #endif //DARWIN
70 
71 #define SG_SET_LOCALE_C setlocale(LC_ALL, "C")
72 #define SG_RESET_LOCALE setlocale(LC_ALL, "")
73 
74 // printf like funktions (with additional severity level)
75 // for object derived from CSGObject
76 #define SG_GCDEBUG(...) io->message(MSG_GCDEBUG, __FILE__, __LINE__, __VA_ARGS__)
77 #define SG_DEBUG(...) io->message(MSG_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
78 #define SG_INFO(...) io->message(MSG_INFO, __FILE__, __LINE__, __VA_ARGS__)
79 #define SG_WARNING(...) io->message(MSG_WARN, __FILE__, __LINE__, __VA_ARGS__)
80 #define SG_ERROR(...) io->message(MSG_ERROR, __FILE__, __LINE__, __VA_ARGS__)
81 #define SG_UNSTABLE(func, ...) io->message(MSG_WARN, __FILE__, __LINE__, \
82 __FILE__ ":" func ": Unstable method! Please report if it seems to " \
83 "work or not to the Shogun mailing list. Thanking you in " \
84 "anticipation. " __VA_ARGS__)
85 
86 #define SG_PRINT(...) io->message(MSG_MESSAGEONLY, __FILE__, __LINE__, __VA_ARGS__)
87 #define SG_NOTIMPLEMENTED io->not_implemented(__FILE__, __LINE__)
88 #define SG_DEPRECATED io->deprecated(__FILE__, __LINE__)
89 
90 #define SG_PROGRESS(...) io->progress(__VA_ARGS__)
91 #define SG_ABS_PROGRESS(...) io->absolute_progress(__VA_ARGS__)
92 #define SG_DONE() io->done()
93 
94 // printf like function using the global sg_io object
95 #define SG_SGCDEBUG(...) sg_io->message(MSG_GCDEBUG,__FILE__, __LINE__, __VA_ARGS__)
96 #define SG_SDEBUG(...) sg_io->message(MSG_DEBUG,__FILE__, __LINE__, __VA_ARGS__)
97 #define SG_SINFO(...) sg_io->message(MSG_INFO,__FILE__, __LINE__, __VA_ARGS__)
98 #define SG_SWARNING(...) sg_io->message(MSG_WARN,__FILE__, __LINE__, __VA_ARGS__)
99 #define SG_SERROR(...) sg_io->message(MSG_ERROR,__FILE__, __LINE__, __VA_ARGS__)
100 #define SG_SPRINT(...) sg_io->message(MSG_MESSAGEONLY,__FILE__, __LINE__, __VA_ARGS__)
101 #define SG_SPROGRESS(...) sg_io->progress(__VA_ARGS__)
102 #define SG_SABS_PROGRESS(...) sg_io->absolute_progress(__VA_ARGS__)
103 #define SG_SDONE() sg_io->done()
104 #define SG_SNOTIMPLEMENTED sg_io->not_implemented(__FILE__, __LINE__)
105 #define SG_SDEPRECATED sg_io->deprecated(__FILE__, __LINE__)
106 
107 #define ASSERT(x) { if (!(x)) SG_SERROR("assertion %s failed in file %s line %d\n",#x, __FILE__, __LINE__);}
108 #define REQUIRE(x, ...) { if (!(x)) SG_SERROR(__VA_ARGS__); }
109 
110 
117 class SGIO
118 {
119  public:
121  SGIO();
123  SGIO(const SGIO& orig);
124 
129  void set_loglevel(EMessageType level);
130 
135  EMessageType get_loglevel() const;
136 
141  inline bool get_show_progress() const
142  {
143  return show_progress;
144  }
145 
150  inline bool get_show_file_and_line() const
151  {
152  return show_file_and_line;
153  }
154 
159  inline bool get_syntax_highlight() const
160  {
161  return syntax_highlight;
162  }
163 
174  void message(EMessageType prio, const char* file,
175  int32_t line, const char *fmt, ... ) const;
176 
185  void progress(
186  float64_t current_val,
187  float64_t min_val=0.0, float64_t max_val=1.0, int32_t decimals=1,
188  const char* prefix="PROGRESS:\t");
189 
199  void absolute_progress(
200  float64_t current_val, float64_t val,
201  float64_t min_val=0.0, float64_t max_val=1.0, int32_t decimals=1,
202  const char* prefix="PROGRESS:\t");
203 
208  void done();
209 
211  inline void not_implemented(const char* file, int32_t line) const
212  {
213  message(MSG_ERROR, file, line, "Sorry, not yet implemented .\n");
214  }
215 
217  inline void deprecated(const char* file, int32_t line) const
218  {
219  message(MSG_WARN, file, line,
220  "This function is deprecated and will be removed soon.\n");
221  }
222 
228  void buffered_message(EMessageType prio, const char *fmt, ... ) const;
229 
235  static char* skip_spaces(char* str);
236 
242  static char* skip_blanks(char* str);
243 
248  inline FILE* get_target() const
249  {
250  return target;
251  }
252 
257  void set_target(FILE* target);
258 
260  inline void set_target_to_stderr() { set_target(stderr); }
261 
263  inline void set_target_to_stdout() { set_target(stdout); }
264 
266  inline void enable_progress()
267  {
268  show_progress=true;
269 
270  // static functions like CSVM::classify_example_helper call SG_PROGRESS
271  if (sg_io!=this)
273  }
274 
276  inline void disable_progress()
277  {
278  show_progress=false;
279 
280  // static functions like CSVM::classify_example_helper call SG_PROGRESS
281  if (sg_io!=this)
283  }
284 
286  inline void enable_file_and_line()
287  {
288  show_file_and_line=true;
289 
290  if (sg_io!=this)
292  }
293 
295  inline void disable_file_and_line()
296  {
297  show_file_and_line=false;
298 
299  if (sg_io!=this)
301  }
302 
305  {
306  syntax_highlight=true;
307 
308  if (sg_io!=this)
310  }
311 
314  {
315  syntax_highlight=false;
316 
317  if (sg_io!=this)
319  }
320 
325  static inline void set_dirname(const char* dirname)
326  {
327  strncpy(directory_name, dirname, FBUFSIZE);
328  }
329 
336  static inline char* concat_filename(const char* filename)
337  {
338  if (snprintf(file_buffer, FBUFSIZE, "%s/%s", directory_name, filename) > FBUFSIZE)
339  SG_SERROR("filename too long");
340  SG_SDEBUG("filename=\"%s\"\n", file_buffer);
341  return file_buffer;
342  }
343 
349  static inline int filter(CONST_DIRENT_T* d)
350  {
351  if (d)
352  {
353  char* fname=concat_filename(d->d_name);
354 
355  if (!access(fname, R_OK))
356  {
357  struct stat s;
358  if (!stat(fname, &s) && S_ISREG(s.st_mode))
359  return 1;
360  }
361  }
362 
363  return 0;
364  }
365 
370  inline int32_t ref()
371  {
372  ++refcount;
373  return refcount;
374  }
375 
380  inline int32_t ref_count() const
381  {
382  return refcount;
383  }
384 
390  inline int32_t unref()
391  {
392  if (refcount==0 || --refcount==0)
393  {
394  delete this;
395  return 0;
396  }
397  else
398  return refcount;
399  }
400 
402  inline const char* get_name() { return "SGIO"; }
403 
404  protected:
411  const char* get_msg_intro(EMessageType prio) const;
412 
413  protected:
415  FILE* target;
429 
437  static const char* message_strings[NUM_LOG_LEVELS];
438 
440  static char file_buffer[FBUFSIZE];
442  static char directory_name[FBUFSIZE];
443 
444  private:
445  int32_t refcount;
446 };
447 
455 struct substring
456 {
458  char *start;
460  char *end;
461 };
462 
463 
470 {
471  uint32_t len = s.end - s.start+1;
472  char* ret = SG_CALLOC(char, len);
473  memcpy(ret,s.start,len-1);
474  return ret;
475 }
476 
482 {
483  char* c_string = c_string_of_substring(s);
484  SG_SPRINT("%s\n", c_string);
485  SG_FREE(c_string);
486 }
487 
495 {
496  char* endptr = s.end;
497  float32_t f = strtof(s.start,&endptr);
498  if (endptr == s.start && s.start != s.end)
499  SG_SERROR("error: %s is not a float!\n", c_string_of_substring(s));
500 
501  return f;
502 }
503 
510 {
511  char* endptr = s.end;
512  float64_t f = strtod(s.start,&endptr);
513  if (endptr == s.start && s.start != s.end)
514  SG_SERROR("Error!:%s is not a double!\n", c_string_of_substring(s));
515 
516  return f;
517 }
518 
524 inline int32_t int_of_substring(substring s)
525 {
526  char* c_string = c_string_of_substring(s);
527  int32_t int_val = atoi(c_string);
528  SG_FREE(c_string);
529 
530  return int_val;
531 }
532 
538 inline uint32_t ulong_of_substring(substring s)
539 {
540  return strtoul(s.start,NULL,10);
541 }
542 
548 inline uint32_t ss_length(substring s)
549 {
550  return (s.end - s.start);
551 }
552 
553 }
554 #endif // __SGIO_H__

SHOGUN Machine Learning Toolbox - Documentation