File: msg.c

package info (click to toggle)
htp 1.11-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 316 kB
  • ctags: 429
  • sloc: ansic: 5,108; makefile: 55
file content (165 lines) | stat: -rw-r--r-- 3,879 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
//
// msg.c
//
// Processing messages (informational, warning, and error)
//
// Copyright (c) 1995-96 Jim Nelson.  Permission to distribute
// granted by the author.  No warranties are made on the fitness of this
// source code.
//
*/

/*
// Because the Linux Gnu compiler doesnt support stdarg.h (at least, the
// copy I have doesnt), htp uses varargs.h, which doesnt support full
// prototyping of variable-argument functions ... this causes tons o' warning
// messages on DOS (and I suspect most ANSI) compilers.  So, to alleviate
// the problem, if this macro is defined, the K&R prototype is used,
// otherwise all other .c modules get the ANSI prototype
*/
#define DUMB_MSG_PROTOTYPE

#include "htp.h"

/* symbols used to express severity of message */
const char *severitySymbol[3] =
{
    "[-]",
    "[*]",
    "[!]",
};

/* the current severity level (any message of this level or higher is printed */
uint severityLevel = MSG_INFO;

/* set severity level */
void SetMessageSeverityLevel(uint level)
{
    if(level > MSG_ERROR)
    {
        severityLevel = MSG_ERROR;
    }
    else
    {
        severityLevel = level;
    }
}

void HtpMsg(va_alist) va_dcl
{
    va_list argptr;
    char *str;
    uint level;
    TEXTFILE *textFile;
    const char *format;

    /* allocate room for string ... 1K should be large enough, but a more */
    /* deterministic method would be better */
    if((str = AllocMemory(1024)) == NULL)
    {
        return;
    }

    /* convert variable arguments into single string */
    va_start(argptr);
    level = va_arg(argptr, uint);
    textFile = va_arg(argptr, TEXTFILE *);
    format = va_arg(argptr, const char *);
    vsprintf(str, format, argptr);
    va_end(argptr);

    /* check severity level */
    if(level < severityLevel)
    {
        FreeMemory(str);
        return;
    }

    /* print the standard message header followed by formatted message */
    printf("%s ", severitySymbol[level]);
    if(textFile != NULL)
    {
        printf("%s line %d: ", textFile->name, textFile->lineNumber);
    }
    printf("%s\n", str);

    /* free the string and exit */
    FreeMemory(str);
}

FILE *debugMsgFile = NULL;

void DebugInit(const char *debugMsgFilename)
{
    remove(debugMsgFilename);
    debugMsgFile = fopen(debugMsgFilename, "at");

    if(debugMsgFile == NULL)
    {
        printf("htp: unable to open debug file \"%s\", aborting\n",
            debugMsgFilename);
        exit(1);
    }
}

void DebugTerminate(void)
{
    if(debugMsgFile != NULL)
    {
        fclose(debugMsgFile);
        debugMsgFile = NULL;
    }
}

/*
// DebugMsg
//
// DebugMsg is a helper function to (a) log a formatted string to disk and
// (b) display the same string to the screen.  This function is called by
// DEBUG_PRINT, which is normally removed from the code in a final release
// build.
//
// Because this debug information is really most useful when the program
// is coughing up blood, this function will flush contents every time,
// doing whatever it can to get the debug string to disk.  Slow, but that's
// what a debug build is for.
//
// Important: this file can't use the abstraction present in textfile.c
// because *that* module uses DEBUG_PRINT as well ... (same for suballoc.c
// functions)
//
*/
void DebugMsg(va_alist) va_dcl
{
    va_list argptr;
    char *str;
    const char *format;

    /* 1K should be enough, but no guarantees */
    if((str = malloc(1024)) == NULL)
    {
        /* !! gaaak */
        return;
    }

    /* convert variable arguments into single string */
    va_start(argptr);
    format = va_arg(argptr, const char *);
    vsprintf(str, format, argptr);
    va_end(argptr);

    /* write the string to disk */
    fprintf(debugMsgFile, str);

    /* flush it out to disk */
    fflush(debugMsgFile);

#if 0
    /* write it to screen */
    printf(str);
#endif

    free(str);
}