]> rtime.felk.cvut.cz Git - orte.git/blob - orte/liborte/debug.c
changed name to Open Real-time Ethernet, some source header arranging
[orte.git] / orte / liborte / debug.c
1 /*
2  *  $Id: debug.c,v 0.0.0.1              2003/08/21
3  *
4  *  DEBUG:  section 1                   Debug
5  *
6  *  -------------------------------------------------------------------  
7  *                                ORTE                                 
8  *                      Open Real-Time Ethernet                       
9  *                                                                    
10  *                      Copyright (C) 2001-2006                       
11  *  Department of Control Engineering FEE CTU Prague, Czech Republic  
12  *                      http://dce.felk.cvut.cz                       
13  *                      http://www.ocera.org                          
14  *                                                                    
15  *  Author:              Petr Smolik    petr.smolik@wo.cz             
16  *  Advisor:             Pavel Pisa                                   
17  *  Project Responsible: Zdenek Hanzalek                              
18  *  --------------------------------------------------------------------
19  *
20  *  This program is free software; you can redistribute it and/or modify
21  *  it under the terms of the GNU General Public License as published by
22  *  the Free Software Foundation; either version 2 of the License, or
23  *  (at your option) any later version.
24  *
25  *  This program is distributed in the hope that it will be useful,
26  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
27  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  *  GNU General Public License for more details.
29  *
30  */
31
32 #include "orte_all.h"
33
34 /* global debug variables */
35 int db_level;
36 int debugLevels[MAX_DEBUG_SECTIONS];
37 int mem_check_counter=0;
38 NtpTime zNtpTime,iNtpTime;
39 SequenceNumber noneSN;
40
41 /*********************************************************************/
42 /* forvard declarations */
43 static void
44 db_print_output(const char *format);
45
46 /*********************************************************************/
47 /* globals */
48
49 #ifndef CONFIG_ORTE_RT
50 FILE *debug_log=NULL;       /* NULL */
51 #endif
52
53 /*********************************************************************/
54 /* functions */
55
56 #ifdef CONFIG_ORTE_RT
57 static const char *
58 debug_log_time(void) {
59   struct timespec        time;
60   static char            buf[64];
61
62   clock_gettime(CLOCK_REALTIME, &time);
63   sprintf(buf, "%li.%03li", time.tv_sec,time.tv_nsec/1000000);
64   return buf;
65 }
66 #else
67 static const char *
68 debug_log_time(void) {
69   struct timeval        time;
70   static char         buf[64];
71
72   gettimeofday(&time,NULL);
73   sprintf(buf, "%li.%03li", time.tv_sec,time.tv_usec/1000);
74   return buf;
75 }
76 #endif /* CONFIG_ORTE_RT */
77
78 void
79 db_print(const char *format,...) {
80   char f[256];
81   va_list ap;
82
83   va_start(ap, format);
84   sprintf(f, "%s | ",debug_log_time());
85   vsprintf(f+strlen(f),format,ap);
86   va_end(ap);
87   db_print_output(f);    
88 }
89
90 void
91 db_print_output(const char *format) {
92 #ifndef CONFIG_ORTE_RT
93   if (debug_log == NULL) return;
94   fprintf(debug_log, format);
95   fflush(debug_log);
96 #else
97   rtl_printf(format);
98 #endif
99 }
100
101 void
102 debug_arg(const char *arg) {
103   int32_t s=0,l=0,i;
104
105   if (!strncmp(arg, "ALL", 3)) {
106     s = -1;
107     arg += 4;
108   } else {
109     s = atoi(arg);
110     while (*arg && *arg++ != '.');
111   }
112   l = atoi(arg);
113   if (l < 0) l = 0;
114   if (l > 10) l = 10;
115   if (s >= 0) {
116     debugLevels[s] = l;
117     return;
118   }
119   for (i = 0; i < MAX_DEBUG_SECTIONS; i++)
120     debugLevels[i] = l;
121
122
123 void 
124 debug_options(const char *options) {
125   char *p = NULL;
126   char *s = NULL;
127
128   if (options) {
129     p=(char*)MALLOC(strlen(options)+1);
130     if (p) {
131       memcpy(p, options, strlen(options) + 1);
132     }    
133     for (s = strtok(p, ":"); s; s = strtok(NULL, ":"))
134       debug_arg(s);
135     FREE(p);
136   }     
137 }
138
139 void
140 debug_open_log(const char *logfile) {
141 #ifndef CONFIG_ORTE_RT
142   if (logfile == NULL) {
143     debug_log = stderr;
144     return;
145   }
146   if (debug_log && debug_log != stderr)
147     fclose(debug_log);
148   debug_log = fopen(logfile, "a+");
149   if (!debug_log) {
150     fprintf(stderr, "WARNING: Cannot write log file: %s\n", logfile);
151     perror(logfile);
152     fprintf(stderr, "         messages will be sent to 'stderr'.\n");
153     fflush(stderr);
154     debug_log = stderr;
155   }
156 #endif
157 }
158
159 void
160 db_init(const char *logfile, const char *options) {
161   int i;
162
163   for (i = 0; i < MAX_DEBUG_SECTIONS; i++)
164     debugLevels[i] = -1;
165   debug_options(options);
166   debug_open_log(logfile);
167 }
168
169 #ifdef ENABLE_MEM_CHECK
170 void *mem_check_malloc(size_t size) { 
171   void *ptr;
172   
173   if ((ptr=malloc(size))) {
174     mem_check_counter++;
175     debug(1,9) ("mem check: inc %d\n",mem_check_counter);
176   }
177   return ptr;
178 }
179
180 void mem_check_free(void *ptr) {
181   if(!ptr) {
182 //    LOG_FATAL(KERN_CRIT "ul_mem_check_free : triing to free NULL ptr\n");
183   }else{
184     mem_check_counter--;
185     debug(1,9) ("mem check: dec %d\n",mem_check_counter);
186     free(ptr);
187   }
188 }
189 #endif /* ENABLE_MEM_CHECK */