All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tprintf.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: tprintf.c
3  * Description: Trace version of printf - portable between UX and NT
4  * Author: Phil Cheatle
5  * Created: Wed Jun 28 15:01:15 BST 1995
6  *
7  * (C) Copyright 1995, Hewlett-Packard Ltd.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 // Include automatically generated configuration file if running autoconf.
21 #ifdef HAVE_CONFIG_H
22 #include "config_auto.h"
23 #endif
24 
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include "ccutil.h"
28 #include "params.h"
29 #include "strngs.h"
30 #include "tprintf.h"
31 
32 #define MAX_MSG_LEN 65536
33 
34 #define EXTERN
35 // Since tprintf is protected by a mutex, these parameters can remain global.
36 DLLSYM STRING_VAR(debug_file, "", "File to send tprintf output to");
37 
38 DLLSYM void
39 tprintf_internal( // Trace printf
40  const char *format, ... // Message
41 ) {
43  va_list args; // variable args
44  static FILE *debugfp = NULL; // debug file
45  // debug window
46  inT32 offset = 0; // into message
47  static char msg[MAX_MSG_LEN + 1];
48 
49  va_start(args, format); // variable list
50  // Format into msg
51  #ifdef _WIN32
52  offset += _vsnprintf(msg + offset, MAX_MSG_LEN - offset, format, args);
53  if (strcmp(debug_file.string(), "/dev/null") == 0)
54  debug_file.set_value("nul");
55  #else
56  offset += vsnprintf(msg + offset, MAX_MSG_LEN - offset, format, args);
57  #endif
58  va_end(args);
59 
60  if (debugfp == NULL && strlen(debug_file.string()) > 0) {
61  debugfp = fopen(debug_file.string(), "wb");
62  } else if (debugfp != NULL && strlen(debug_file.string()) == 0) {
63  fclose(debugfp);
64  debugfp = NULL;
65  }
66  if (debugfp != NULL)
67  fprintf(debugfp, "%s", msg);
68  else
69  fprintf(stderr, "%s", msg);
71 }
#define MAX_MSG_LEN
Definition: tprintf.cpp:32
CCUtilMutex tprintfMutex
Definition: ccutil.cpp:51
DLLSYM void tprintf_internal(const char *format,...)
Definition: tprintf.cpp:39
#define STRING_VAR(name, val, comment)
Definition: params.h:283
#define NULL
Definition: host.h:144
DLLSYM char * debug_file
Definition: tprintf.cpp:36
#define DLLSYM
Definition: platform.h:25
int inT32
Definition: host.h:102