OSDN Git Service

add doxycomment and copyright header
[ccunit/ccunit.git] / src / ccunit / CCUnitLogMessage.c
1 /* Copyright (C) 2003 TSUTSUMI Kikuo.
2    This file is part of the CCUnit Library.
3
4    The CCUnit Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public License
6    as published by the Free Software Foundation; either version 2.1 of
7    the License, or (at your option) any later version.
8
9    The CCUnit Library is distributed in the hope that it will be
10    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
11    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the CCUnit Library; see the file COPYING.LESSER.
16    If not, write to the Free Software Foundation, Inc., 59 Temple
17    Place - Suite 330, Boston, MA 02111-1307, USA.  
18 */
19
20 /*
21  * $Id$
22  */
23
24 #include <ccunit/CCUnitLogMessage.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27
28 /**
29  * debug message flag.
30  */
31 bool _ccunit_debug_message = false;
32
33 /**
34  * verbose message flag.
35  */
36 bool _ccunit_verbose_message = false;
37
38 /**
39  * return stack growing direction.
40  *
41  * @param caller stack variable address of caller function.
42  *
43  * @return 1 when stack growing up, -1 when stack growing down.
44  */
45 static int stackdir (const int* caller)
46 {
47   auto const int stack = 0;
48   return caller < &stack ? 1 : -1;
49 }
50
51 /**
52  * count msg function call nestings
53  *
54  * @return 1 when function nested, -1 when function returned, 0 when
55  * function is same level.
56  */
57 static int nestings ()
58 {
59   auto const int stack = 0;
60   static const int* previous = NULL;
61   static int dir = 0;
62   int nest = 0;
63   if (!dir)
64     dir = stackdir (&stack);
65   if (!previous)
66     nest = 0;
67   else if (previous < &stack)
68     nest = dir;
69   else if (previous > &stack)
70     nest = -dir;
71   else
72     nest = 0;
73   previous = &stack;
74   return nest;
75 }
76
77 /**
78  * print message.
79  *
80  * @param prompt message prompt string.
81  * @param fmt message format string for printf.
82  * @param args message arguments.
83  */
84 static void msg (const char* prompt, const char* fmt, va_list args)
85 {
86   static int leader = 1;
87   const int inc = 2;
88   leader += nestings () * inc;
89   fprintf (stderr, "%s:%*c", prompt, leader, ' ');
90   vfprintf (stderr, fmt, args);
91   fputc ('\n', stderr);
92 }
93
94 inline void ccunit_dbg (const char* fmt, ...)
95 {
96   if (_ccunit_debug_message)
97     {
98       va_list args;
99       va_start (args, fmt);
100       msg ("DBG", fmt, args);
101       va_end (args);
102     }
103 }
104
105 inline void ccunit_log (const char* fmt, ...)
106 {
107   if (_ccunit_verbose_message || _ccunit_debug_message)
108     {
109       va_list args;
110       va_start (args, fmt);
111       msg ("LOG", fmt, args);
112       va_end (args);
113     }
114 }
115
116 inline void ccunit_err (const char* fmt, ...)
117 {
118   va_list args;
119   va_start (args, fmt);
120   msg ("ERR", fmt, args);
121   va_end (args);
122 }
123
124 inline void ccunit_msg (const char* fmt, ...)
125 {
126   va_list args;
127   va_start (args, fmt);
128   msg ("CCUNIT", fmt, args);
129   va_end (args);
130 }