root/trunk/txlib/txlogfil.c

Revision 14, 7.8 kB (checked in by jvw, 2 years ago)

HEX/ASCII (sector) editor control added

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// file-logging facilities
34//
35// Author: J. van Wijk
36//
37// JvW  26-12-96   Split-off from ansilog
38
39#include <txlib.h>
40
41static  FILE      *log_handle  = 0;
42static  TXLN       log_fname   = "logfile";
43static  BOOL       log_7bit    = TRUE;
44static  BOOL       log_reopen  = FALSE;         // reopen on each line
45
46/*****************************************************************************/
47// Close existing and if specified, open new logfile
48/*****************************************************************************/
49BOOL TxAppendToLogFile                          // RET   logfile opened
50(
51   char               *fname,                   // IN    name of (new) logfile
52   BOOL                verbose                  // IN    Show the action
53)
54{
55   if (log_handle != 0)                         // close old one
56   {
57      if (verbose)
58      {
59         TxPrint("Closing logfile   : '%s'\n", log_fname);
60      }
61      fclose(log_handle);
62      log_handle = 0;
63   }
64   if (fname && strlen(fname))
65   {
66      if (strcmp( fname, "."))                  // not 'current one' ?
67      {
68         strcpy( log_fname, fname);             // use new supplied name
69         TxFnameExtension( log_fname, "log");
70      }
71      if (((log_handle = fopen(log_fname, "a" TXFMODE)) == 0) && (verbose))
72      {
73         TxPrint("Error opening log : '%s'\n", log_fname);
74      }
75      else
76      {
77         TxLogfileState( DEVICE_ON);            // always ON for new file
78         if (verbose)                           // avoid any TxPrint when
79         {                                      // working in quiet mode
80            if (TxaOptUnSet('7'))               // NO strip to 7-bit ASCII
81            {
82               log_7bit = FALSE;
83            }
84            if (TxaExeArgc() != 0)              // Switches already parsed ?
85            {                                   // (-q = quiet switch to come)
86               TxPrint("Appending to log  : '%s' (%s-bit ASCII)\n",
87                         log_fname, (log_7bit) ? "7" : "8");
88            }
89         }
90      }
91   }
92   return( log_handle != 0);
93}                                               // end 'TxAppendToLogFile'
94/*---------------------------------------------------------------------------*/
95
96
97/*****************************************************************************/
98// Set log close/reopen on each TxPrint on or off
99/*****************************************************************************/
100void TxSetLogReOpen
101(
102   BOOL                reopen                   // IN    log reopen on
103)
104{
105   log_reopen = reopen;
106}                                               // end 'TxSetLogReOpen'
107/*---------------------------------------------------------------------------*/
108
109
110/*****************************************************************************/
111// Close existing and reopen (implementing a brute-force flush)
112/*****************************************************************************/
113void TxCloseReopenLogFile
114(
115   void
116)
117{
118   static BOOL         warn = TRUE;
119
120   if (log_handle != 0)
121   {
122      fclose(log_handle);
123      if ((log_handle = fopen(log_fname, "a" TXFMODE)) == 0)
124      {
125         if (warn)
126         {
127            TxPrint("\nError re-opening  : '%s'\n", log_fname);
128            warn = FALSE;                       // I will say this only once!
129         }
130      }
131   }
132}                                               // end 'TxCloseReopenLogFile'
133/*---------------------------------------------------------------------------*/
134
135
136/*****************************************************************************/
137// Signal logfile active
138/*****************************************************************************/
139FILE *TxQueryLogFile                            // RET   logfile active
140(
141   BOOL               *ascii7bit,               // OUT   Use 7-bit ASCII only
142   BOOL               *reopenlog                // OUT   reopen logfile
143)                                               //       NULL if not wanted
144{
145   if (ascii7bit != NULL)
146   {
147      *ascii7bit  = log_7bit;
148   }
149   if (reopenlog != NULL)
150   {
151      *reopenlog  = log_reopen;
152   }
153   return( log_handle);
154}                                               // end 'TxQueryLogFile'
155/*---------------------------------------------------------------------------*/
156
157
158/*****************************************************************************/
159// Get name for current logfile, if any
160/*****************************************************************************/
161char *TxQueryLogName                            // RET   filename or NULL
162(
163   void
164)                                               //       (NULL if not wanted)
165{
166   char               *rc = NULL;
167
168   if (log_handle != 0)
169   {
170      rc = log_fname;
171   }
172   return (rc);
173}                                               // end 'TxQueryLogName'
174/*---------------------------------------------------------------------------*/
175
176
177/*****************************************************************************/
178// Construct name for new logfile based upon a sequence number 0..n
179/*****************************************************************************/
180char *TxBuildLogName                            // RET   filename or NULL
181(
182   ULONG               seq,                     // IN    sequence number
183   ULONG               retain,                  // IN    nr of files kept
184   TXLN                buf                      // IN    filename buffer
185)
186{
187   char               *rc = buf;
188   TXTS                ext;
189
190   if (seq == 0)                                // first will be .log
191   {
192      strcpy( ext, "log");
193   }
194   else
195   {
196      if ((seq >= 100) && (retain < 100))       // OK to use modulo seq ?
197      {
198         seq %= 100;                            // reduce to two digits, to
199      }                                         // limit extension to three
200      sprintf( ext, "l%02lu", seq);
201   }
202   strcpy( rc, log_fname);
203
204   TxStripExtension( rc);                       // remove current extension
205   TxFnameExtension( rc, ext);                  // and add new one
206
207   return (rc);
208}                                               // end 'TxBuildLogName'
209/*---------------------------------------------------------------------------*/
Note: See TracBrowser for help on using the browser.