File: logging.c

package info (click to toggle)
liblouis 3.0.0-3%2Bdeb9u4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 68,336 kB
  • sloc: ansic: 26,525; sh: 5,113; makefile: 739; perl: 69; python: 42
file content (183 lines) | stat: -rw-r--r-- 4,416 bytes parent folder | download
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* liblouis Braille Translation and Back-Translation  Library

Copyright (C) 2004, 2005, 2006 ViewPlus Technologies, Inc. www.viewplus.com
Copyright (C) 2004, 2005, 2006 JJB Software, Inc. www.jjb-software.com

This file is part of liblouis.

liblouis is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 2.1 of the License, or
(at your option) any later version.

liblouis is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with liblouis. If not, see <http://www.gnu.org/licenses/>.

*/

/**
 * @file
 * @brief Logging
 */

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#include "louis.h"

void logWidecharBuf(logLevels level, const char *msg, const widechar *wbuf, int wlen)
{
  /* When calculating output size:
   * Each wdiechar is represented in hex, thus needing two bytes for each
   * byte in the widechar (sizeof(widechar) * 2)
   * Allow space for the "0x%X " formatting (+ 3)
   * Number of characters in widechar buffer (wlen * )
   * Give space for additional message (+ strlen(msg))
   * Remember the null terminator (+ 1)
   */
  int logBufSize = (wlen * ((sizeof(widechar) * 3) + 3)) + 3 + (int)strlen(msg);
  char *logMsg = malloc(logBufSize);
  char *p = logMsg;
  char *formatString;
  int i = 0;
  if (sizeof(widechar) == 2)
    formatString = "0x%04X ";
  else
    formatString = "0x%08X ";
  for (i = 0; i < (int) strlen(msg); i++)
    logMsg[i] = msg[i];
  p += strlen(msg);
  for (i = 0; i < wlen; i++)
    {
      p += sprintf(p, formatString, wbuf[i]);
    }
	*p = '~';
	p++;
	*p = ' ';
	p++;
	for(i = 0; i < wlen; i++)
	{
		if(wbuf[i] & 0xff00)
			*p = ' ';
		else
			*p = (char)wbuf[i];
		p++;
	}	
  *p = '\0';
  logMessage(level, "%s", logMsg);
  free(logMsg);
}

static void defaultLogCallback(logLevels level, const char *message)
{
  lou_logPrint("%s", message); // lou_logPrint takes formatting, protect against % in message
}

static logcallback logCallbackFunction = defaultLogCallback;
void EXPORT_CALL lou_registerLogCallback(logcallback callback)
{
  if (callback == NULL)
    logCallbackFunction = defaultLogCallback;
  else
    logCallbackFunction = callback;
}

static logLevels logLevel = LOG_INFO;
void EXPORT_CALL lou_setLogLevel(logLevels level)
{
  logLevel = level;
}

void logMessage(logLevels level, const char *format, ...)
{
  if (format == NULL)
      return;
  if (level < logLevel)
      return;
  if (logCallbackFunction != NULL)
    {
#ifdef _WIN32
      double f = 2.3; // Needed to force VC++ runtime floating point support
#endif
      char *s;
      size_t len;
      va_list argp;
      va_start(argp, format);
      len = vsnprintf(0, 0, format, argp);
      va_end(argp);
      if ((s = malloc(len+1)) != 0)
        {
          va_start(argp, format);
          vsnprintf(s, len+1, format, argp);
          va_end(argp);
          logCallbackFunction(level, s);
          free(s);
        }
    }
}


static FILE *logFile = NULL;
static char initialLogFileName[256] = "";

void EXPORT_CALL
lou_logFile (const char *fileName)
{
	if(logFile)
	{
		fclose(logFile);
		logFile = NULL;
	}
  if (fileName == NULL || fileName[0] == 0)
    return;
  if (initialLogFileName[0] == 0)
    strcpy (initialLogFileName, fileName);
  logFile = fopen (fileName, "a");
  if (logFile == NULL && initialLogFileName[0] != 0)
    logFile = fopen (initialLogFileName, "a");
  if (logFile == NULL)
    {
      fprintf (stderr, "Cannot open log file %s\n", fileName);
      logFile = stderr;
    }
}

void EXPORT_CALL
lou_logPrint (const char *format, ...)
{
#ifndef __SYMBIAN32__
  va_list argp;
  if (format == NULL)
    return;
  if (logFile == NULL)
    logFile = fopen (initialLogFileName, "a");
  if (logFile == NULL)
    logFile = stderr;
  va_start (argp, format);
  vfprintf (logFile, format, argp);
  fprintf (logFile, "\n");
  fflush(logFile);
  va_end (argp);
#endif
}

void EXPORT_CALL
lou_logEnd ()
{
  closeLogFile();
}

void closeLogFile()
{
  if (logFile != NULL && logFile != stderr)
    fclose (logFile);
  logFile = NULL;
}