root/trunk/txlib/txtrace.c

Revision 13, 8.7 kB (checked in by jvw, 3 years ago)

Logfile size-limit and automatic logfile rotation, very useful for crash tracing

Line 
1//
2//                     TxWin, Textmode Windowing Library
3//
4//   Original code Copyright (c) 1995-2005 Fsys Software and Jan van Wijk
5//
6// ==========================================================================
7//
8// This file contains Original Code and/or Modifications of Original Code as
9// defined in and that are subject to the GNU Lesser General Public License.
10// You may not use this file except in compliance with the License.
11// BY USING THIS FILE YOU AGREE TO ALL TERMS AND CONDITIONS OF THE LICENSE.
12// A copy of the License is provided with the Original Code and Modifications,
13// and is also available at http://www.dfsee.com/txwin/lgpl.htm
14//
15// This library is free software; you can redistribute it and/or modify
16// it under the terms of the GNU Lesser General Public License as published
17// by the Free Software Foundation; either version 2.1 of the License,
18// or (at your option) any later version.
19//
20// This library is distributed in the hope that it will be useful,
21// but WITHOUT ANY WARRANTY; without even the implied warranty of
22// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23// See the GNU Lesser General Public License for more details.
24//
25// You should have received a copy of the GNU Lesser General Public License
26// along with this library; (lgpl.htm) if not, write to the Free Software
27// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28//
29// Questions on TxWin licensing can be directed to: txwin@fsys.nl
30//
31// ==========================================================================
32//
33// Trace functions for module entry/exit
34//
35// Author: J. van Wijk
36//
37// JvW  26-12-1996   Split-off from ansilog
38// JvW  11-10-1999   No timestamp when tracelevel >= 900
39
40#define DUMP 1                                  // tracecode always available
41
42#include <txlib.h>
43#include <txtpriv.h>
44
45        ULONG     TxTrLevel      = 0;           // trace level
46        ULONG     TxTrSlowDown   = 0;           // xx ms pause per traceline
47        BOOL      TxTrLogOnly    = TRUE;        // default to log only
48        BOOL      TxTrTstamp     = FALSE;       // default no timestamp
49
50        ULONG     TxTrIndent[TXTHREADS] = {0};  // indent per thread
51
52static  ULONG     TxTrMainThread = 1;           // TID of main() thread
53
54
55/*****************************************************************************/
56// Init trace level and destinations at startup, to be called from main()
57/*****************************************************************************/
58void TxTraceInitMain
59(
60   int                *pargc,                   // INOUT argument count
61   char              **pargv[],                 // INOUT array of arg values
62   char               *envname,                 // IN    trace env-var name
63   char               *prefix                   // IN    tracefile prefix
64)
65{
66   char               *tr;                      // trace specification
67   int                 argc = *pargc;
68   char              **argv = *pargv;
69   char               *s;
70
71   TxTrMainThread = TXTHREADID;                 // remember TID of main()
72
73   if ((          argc     >  1 ) &&            // there is a parameter
74       (strlen(   argv[1]) >  2 ) &&            // 3 or more characters
75       (         *argv[1] == '-') &&            // and it is a switch
76       (isdigit(*(argv[1] +1) ) ) &&            // and starts numerical
77       (isdigit(*(argv[1] +2) ) )  )            // with at least two digits
78   {
79      tr = argv[1] +1;                          // start trace specification
80
81      *pargc = argc -1;                         // shift rest of arguments
82      *pargv = argv +1;
83   }
84   else
85   {
86      if ((tr = getenv( envname)) != NULL)      // trace in environment ?
87      {
88         int           i;
89
90         for (i = 1; i < argc; i++)
91         {
92            if (strncmp(argv[i], "-l", 2) == 0) // there is a -l switch!
93            {
94               tr = NULL;                       // NO trace now, to avoid
95               break;                           // clobbering real logfile
96            }
97         }
98      }
99   }
100   if (tr != NULL)
101   {
102      TxTrLevel = atol( tr);                    // numeric trace level
103      if (strchr( tr, 's') != NULL)             // screen output too
104      {
105         TxTrLogOnly  = FALSE;
106      }
107      if (strchr( tr, 'r') != NULL)             // close/reopen logfile
108      {
109         TxSetLogReOpen( TRUE);
110      }
111      if ((s = strchr( tr, 'd')) != NULL)       // 100ms pause per traceline
112      {
113         if ((TxTrSlowDown = atol( s+1)) == 0)  // or specific value
114         {
115            TxTrSlowDown = 100;
116         }
117      }
118      if (strchr( tr, 't') != NULL)             // timestamps included
119      {
120         TxTrTstamp   = TRUE;
121      }
122      if (strchr( tr, 'l') == NULL)             // auto-log allowed
123      {
124         TXTM           trcfile;
125
126         sprintf( trcfile, "%s-%lu", prefix, TxTrLevel);
127         TxAppendToLogFile( trcfile, TRUE);
128      }
129   }
130}                                               // end 'TxTraceInitMain'
131/*---------------------------------------------------------------------------*/
132
133
134/*****************************************************************************/
135// Handle entry for a routine
136/*****************************************************************************/
137void TxTraceEnter
138(
139   char              *mod                       // IN    module/function name
140)
141{
142   TxTraceLeader();
143   TxPrint( "%sEnter func%s : %s%s%s\n", CNM, CNN, CNC, mod, CNN);
144   TxTrIndent[ TXTHREADID]++;
145}                                               // end 'TxTraceEnter'
146/*---------------------------------------------------------------------------*/
147
148
149/*****************************************************************************/
150// Handle return of a routine
151/*****************************************************************************/
152void TxTraceReturn
153(
154   char              *mod,                      // IN    module/function name
155   ULONG              rc                        // IN    return value
156)
157{
158   if (TxTrIndent[TXTHREADID])
159   {
160      TxTrIndent[TXTHREADID]--;
161   }
162   TxTraceLeader();
163   TxPrint( "%sRet%s:%s% 8.8lX %s%s%s\n",
164            CNM, CNN, ((rc) ? CBR : CBG), rc, CNC, mod, CNN);
165}                                               // end 'TxTraceReturn'
166/*---------------------------------------------------------------------------*/
167
168
169/*****************************************************************************/
170// Handle boolean return of a routine
171/*****************************************************************************/
172void TxTraceRetBool
173(
174   char              *mod,                      // IN    module/function name
175   BOOL               rc                        // IN    return value
176)
177{
178   if (TxTrIndent[TXTHREADID])
179   {
180      TxTrIndent[TXTHREADID]--;
181   }
182   TxTraceLeader();
183   TxPrint( "%sRet%s:%s%s %s%s%s\n", CNM, CNN,
184            ((rc) ?  CBG       :  CBR),
185            ((rc) ? "True    " : "False   "),
186              CNC,   mod,         CNN);
187}                                               // end 'TxTraceRetBool'
188/*---------------------------------------------------------------------------*/
189
190
191/*****************************************************************************/
192// Handle return of a void routine
193/*****************************************************************************/
194void TxTraceRetVoid
195(
196   char              *mod                       // IN    module/function name
197)
198{
199   if (TxTrIndent[TXTHREADID])
200   {
201      TxTrIndent[ TXTHREADID]--;
202   }
203   TxTraceLeader();
204   TxPrint( "%sReturn void%s: %s%s%s\n", CNM, CNN, CNC, mod, CNN);
205}                                               // end 'TxTraceRetVoid'
206/*---------------------------------------------------------------------------*/
207
208
209/*****************************************************************************/
210// Timestamp, thread-id and indent for trace-line
211/*****************************************************************************/
212void TxTraceLeader
213(
214   void
215)
216{
217   TXTS                lti = {0};
218   TXTT                ltm = {0};
219   ULONG               tid = TXTHREADID;
220
221   if (TxTrSlowDown)
222   {
223      TxSleep( TxTrSlowDown);
224   }
225   if (TxTrTstamp)
226   {
227      sprintf( ltm, "%s%7.7lu ", CBZ, clock());
228   }
229   if ((tid != TxTrMainThread) || (TxTrTstamp))
230   {
231      sprintf( lti, "%2.2lu ", tid);
232   }
233   TxPrint( "%s%s%s%*.*s",  ltm, lti, CNN, (int) TxTrIndent[tid],
234                                            (int) TxTrIndent[tid], "");
235}                                               // end 'TxTraceLeader'
236/*---------------------------------------------------------------------------*/
Note: See TracBrowser for help on using the browser.