SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 #define CONST_DIRENT_T struct dirent
62 #else //DARWIN
63 #define CONST_DIRENT_T const struct dirent
64 #endif //DARWIN
65 
66 #define SG_SET_LOCALE_C setlocale(LC_ALL, "C")
67 #define SG_RESET_LOCALE setlocale(LC_ALL, "")
68 
69 // printf like funktions (with additional severity level)
70 // for object derived from CSGObject
71 #define SG_GCDEBUG(...) io->message(MSG_GCDEBUG, __FILE__, __LINE__, __VA_ARGS__)
72 #define SG_DEBUG(...) io->message(MSG_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
73 #define SG_INFO(...) io->message(MSG_INFO, __FILE__, __LINE__, __VA_ARGS__)
74 #define SG_WARNING(...) io->message(MSG_WARN, __FILE__, __LINE__, __VA_ARGS__)
75 #define SG_ERROR(...) io->message(MSG_ERROR, __FILE__, __LINE__, __VA_ARGS__)
76 #define SG_UNSTABLE(func, ...) io->message(MSG_WARN, __FILE__, __LINE__, \
77 __FILE__ ":" func ": Unstable method! Please report if it seems to " \
78 "work or not to the Shogun mailing list. Thanking you in " \
79 "anticipation. " __VA_ARGS__)
80 
81 #define SG_PRINT(...) io->message(MSG_MESSAGEONLY, __FILE__, __LINE__, __VA_ARGS__)
82 #define SG_NOTIMPLEMENTED io->not_implemented(__FILE__, __LINE__)
83 #define SG_DEPRECATED io->deprecated(__FILE__, __LINE__)
84 
85 #define SG_PROGRESS(...) io->progress(__VA_ARGS__)
86 #define SG_ABS_PROGRESS(...) io->absolute_progress(__VA_ARGS__)
87 #define SG_DONE() io->done()
88 
89 // printf like function using the global sg_io object
90 #define SG_SGCDEBUG(...) sg_io->message(MSG_GCDEBUG,__FILE__, __LINE__, __VA_ARGS__)
91 #define SG_SDEBUG(...) sg_io->message(MSG_DEBUG,__FILE__, __LINE__, __VA_ARGS__)
92 #define SG_SINFO(...) sg_io->message(MSG_INFO,__FILE__, __LINE__, __VA_ARGS__)
93 #define SG_SWARNING(...) sg_io->message(MSG_WARN,__FILE__, __LINE__, __VA_ARGS__)
94 #define SG_SERROR(...) sg_io->message(MSG_ERROR,__FILE__, __LINE__, __VA_ARGS__)
95 #define SG_SPRINT(...) sg_io->message(MSG_MESSAGEONLY,__FILE__, __LINE__, __VA_ARGS__)
96 #define SG_SPROGRESS(...) sg_io->progress(__VA_ARGS__)
97 #define SG_SABS_PROGRESS(...) sg_io->absolute_progress(__VA_ARGS__)
98 #define SG_SDONE() sg_io->done()
99 #define SG_SNOTIMPLEMENTED sg_io->not_implemented(__FILE__, __LINE__)
100 #define SG_SDEPRECATED sg_io->deprecated(__FILE__, __LINE__)
101 
102 #define ASSERT(x) { if (!(x)) SG_SERROR("assertion %s failed in file %s line %d\n",#x, __FILE__, __LINE__);}
103 
104 
111 class SGIO
112 {
113  public:
115  SGIO();
117  SGIO(const SGIO& orig);
118 
123  void set_loglevel(EMessageType level);
124 
129  EMessageType get_loglevel() const;
130 
135  inline bool get_show_progress() const
136  {
137  return show_progress;
138  }
139 
144  inline bool get_show_file_and_line() const
145  {
146  return show_file_and_line;
147  }
148 
153  inline bool get_syntax_highlight() const
154  {
155  return syntax_highlight;
156  }
157 
168  void message(EMessageType prio, const char* file,
169  int32_t line, const char *fmt, ... ) const;
170 
179  void progress(
180  float64_t current_val,
181  float64_t min_val=0.0, float64_t max_val=1.0, int32_t decimals=1,
182  const char* prefix="PROGRESS:\t");
183 
193  void absolute_progress(
194  float64_t current_val, float64_t val,
195  float64_t min_val=0.0, float64_t max_val=1.0, int32_t decimals=1,
196  const char* prefix="PROGRESS:\t");
197 
202  void done();
203 
205  inline void not_implemented(const char* file, int32_t line) const
206  {
207  message(MSG_ERROR, file, line, "Sorry, not yet implemented .\n");
208  }
209 
211  inline void deprecated(const char* file, int32_t line) const
212  {
213  message(MSG_WARN, file, line,
214  "This function is deprecated and will be removed soon.\n");
215  }
216 
222  void buffered_message(EMessageType prio, const char *fmt, ... ) const;
223 
229  static char* skip_spaces(char* str);
230 
236  static char* skip_blanks(char* str);
237 
242  inline FILE* get_target() const
243  {
244  return target;
245  }
246 
251  void set_target(FILE* target);
252 
254  inline void set_target_to_stderr() { set_target(stderr); }
255 
257  inline void set_target_to_stdout() { set_target(stdout); }
258 
260  inline void enable_progress()
261  {
262  show_progress=true;
263 
264  // static functions like CSVM::classify_example_helper call SG_PROGRESS
265  if (sg_io!=this)
267  }
268 
270  inline void disable_progress()
271  {
272  show_progress=false;
273 
274  // static functions like CSVM::classify_example_helper call SG_PROGRESS
275  if (sg_io!=this)
277  }
278 
280  inline void enable_file_and_line()
281  {
282  show_file_and_line=true;
283 
284  if (sg_io!=this)
286  }
287 
289  inline void disable_file_and_line()
290  {
291  show_file_and_line=false;
292 
293  if (sg_io!=this)
295  }
296 
299  {
300  syntax_highlight=true;
301 
302  if (sg_io!=this)
304  }
305 
308  {
309  syntax_highlight=false;
310 
311  if (sg_io!=this)
313  }
314 
319  static inline void set_dirname(const char* dirname)
320  {
321  strncpy(directory_name, dirname, FBUFSIZE);
322  }
323 
330  static inline char* concat_filename(const char* filename)
331  {
332  if (snprintf(file_buffer, FBUFSIZE, "%s/%s", directory_name, filename) > FBUFSIZE)
333  SG_SERROR("filename too long");
334  SG_SDEBUG("filename=\"%s\"\n", file_buffer);
335  return file_buffer;
336  }
337 
343  static inline int filter(CONST_DIRENT_T* d)
344  {
345  if (d)
346  {
347  char* fname=concat_filename(d->d_name);
348 
349  if (!access(fname, R_OK))
350  {
351  struct stat s;
352  if (!stat(fname, &s) && S_ISREG(s.st_mode))
353  return 1;
354  }
355  }
356 
357  return 0;
358  }
359 
364  inline int32_t ref()
365  {
366  ++refcount;
367  return refcount;
368  }
369 
374  inline int32_t ref_count() const
375  {
376  return refcount;
377  }
378 
384  inline int32_t unref()
385  {
386  if (refcount==0 || --refcount==0)
387  {
388  delete this;
389  return 0;
390  }
391  else
392  return refcount;
393  }
394 
396  inline const char* get_name() { return "SGIO"; }
397 
398  protected:
405  const char* get_msg_intro(EMessageType prio) const;
406 
407  protected:
409  FILE* target;
423 
431  static const char* message_strings[NUM_LOG_LEVELS];
432 
434  static char file_buffer[FBUFSIZE];
436  static char directory_name[FBUFSIZE];
437 
438  private:
439  int32_t refcount;
440 };
441 
449 struct substring
450 {
452  char *start;
454  char *end;
455 };
456 
457 
464 {
465  uint32_t len = s.end - s.start+1;
466  char* ret = SG_CALLOC(char, len);
467  memcpy(ret,s.start,len-1);
468  return ret;
469 }
470 
476 {
477  char* c_string = c_string_of_substring(s);
478  SG_SPRINT("%s\n", c_string);
479  SG_FREE(c_string);
480 }
481 
489 {
490  char* endptr = s.end;
491  float32_t f = strtof(s.start,&endptr);
492  if (endptr == s.start && s.start != s.end)
493  SG_SERROR("error: %s is not a float!\n", c_string_of_substring(s));
494 
495  return f;
496 }
497 
504 {
505  char* endptr = s.end;
506  float64_t f = strtod(s.start,&endptr);
507  if (endptr == s.start && s.start != s.end)
508  SG_SERROR("Error!:%s is not a double!\n", c_string_of_substring(s));
509 
510  return f;
511 }
512 
518 inline int32_t int_of_substring(substring s)
519 {
520  char* c_string = c_string_of_substring(s);
521  int32_t int_val = atoi(c_string);
522  SG_FREE(c_string);
523 
524  return int_val;
525 }
526 
532 inline uint32_t ulong_of_substring(substring s)
533 {
534  return strtoul(s.start,NULL,10);
535 }
536 
542 inline uint32_t ss_length(substring s)
543 {
544  return (s.end - s.start);
545 }
546 
547 }
548 #endif // __SGIO_H__

SHOGUN Machine Learning Toolbox - Documentation