]> rtime.felk.cvut.cz Git - orte.git/blob - orte/liborte/debug.c
Add shell.nix
[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@smoliku.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 {
60   struct timespec        time;
61   static char            buf[64];
62
63   clock_gettime(CLOCK_REALTIME, &time);
64   sprintf(buf, "%li.%03li", time.tv_sec, time.tv_nsec/1000000);
65   return buf;
66 }
67 #else
68 static const char *
69 debug_log_time(void)
70 {
71   struct timeval        time;
72   static char         buf[64];
73
74   gettimeofday(&time, NULL);
75   sprintf(buf, "%li.%03li", time.tv_sec, time.tv_usec/1000);
76   return buf;
77 }
78 #endif /* CONFIG_ORTE_RT */
79
80 void
81 db_print(const char *format, ...)
82 {
83   char f[256];
84   va_list ap;
85
86   va_start(ap, format);
87   sprintf(f, "%s | ", debug_log_time());
88   vsprintf(f+strlen(f), format, ap);
89   va_end(ap);
90   db_print_output(f);
91 }
92
93 void
94 db_print_output(const char *format)
95 {
96 #ifndef CONFIG_ORTE_RT
97   if (debug_log == NULL)
98     return;
99   fprintf(debug_log, "%s", format);
100   fflush(debug_log);
101 #else
102   rtl_printf(format);
103 #endif
104 }
105
106 void
107 debug_arg(const char *arg)
108 {
109   int32_t s = 0, l = 0, i;
110
111   if (!strncmp(arg, "ALL", 3)) {
112     s = -1;
113     arg += 4;
114   } else {
115     s = atoi(arg);
116     while (*arg && *arg++ != '.') ;
117   }
118   l = atoi(arg);
119   if (l < 0)
120     l = 0;
121   if (l > 10)
122     l = 10;
123   if (s >= 0) {
124     debugLevels[s] = l;
125     return;
126   }
127   for (i = 0; i < MAX_DEBUG_SECTIONS; i++)
128     debugLevels[i] = l;
129 }
130
131 void
132 debug_options(const char *options)
133 {
134   char *p = NULL;
135   char *s = NULL;
136
137   if (options) {
138     p = (char *)MALLOC(strlen(options)+1);
139     if (p) {
140       memcpy(p, options, strlen(options) + 1);
141     }
142     for (s = strtok(p, ":"); s; s = strtok(NULL, ":"))
143       debug_arg(s);
144     FREE(p);
145   }
146 }
147
148 void
149 debug_open_log(const char *logfile)
150 {
151 #ifndef CONFIG_ORTE_RT
152   if (logfile == NULL) {
153     debug_log = stderr;
154     return;
155   }
156   if (debug_log && debug_log != stderr)
157     fclose(debug_log);
158   debug_log = fopen(logfile, "a+");
159   if (!debug_log) {
160     fprintf(stderr, "WARNING: Cannot write log file: %s\n", logfile);
161     perror(logfile);
162     fprintf(stderr, "         messages will be sent to 'stderr'.\n");
163     fflush(stderr);
164     debug_log = stderr;
165   }
166 #endif
167 }
168
169 void
170 db_init(const char *logfile, const char *options)
171 {
172   int i;
173
174   for (i = 0; i < MAX_DEBUG_SECTIONS; i++)
175     debugLevels[i] = -1;
176   debug_options(options);
177   debug_open_log(logfile);
178 }
179
180 #ifdef ENABLE_MEM_CHECK
181 void *
182 mem_check_malloc(size_t size)
183 {
184   void *ptr;
185
186   if ((ptr = malloc(size))) {
187     mem_check_counter++;
188     debug(1, 9) ("mem check: inc %d\n", mem_check_counter);
189   }
190   return ptr;
191 }
192
193 void
194 mem_check_free(void *ptr)
195 {
196   if (!ptr) {
197 //    LOG_FATAL(KERN_CRIT "ul_mem_check_free : triing to free NULL ptr\n");
198   } else {
199     mem_check_counter--;
200     debug(1, 9) ("mem check: dec %d\n", mem_check_counter);
201     free(ptr);
202   }
203 }
204 #endif /* ENABLE_MEM_CHECK */