summaryrefslogtreecommitdiff
path: root/tests/log/test_log.c
blob: e1486d86690022253d34d455c1e3b6c61c8d732c (plain)
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
/*
    Copyright (C) 2008 Andreas Baumann <abaumann@yahoo.com>

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

    This program 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 General Public License for more details.

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

#include "log/log.h"			/* for logging */
#include "log/messages.h"		/* for i18n */
#include "port/string.h"		/* for strerror_r */
#include "errors.h"

#include <stdlib.h>			/* for exit, EXIT_SUCCESS, free */
#include <errno.h>			/* for errno */

#ifdef _WIN32
#define WIND32_LEAN_AND_MEAN
#include <windows.h>
#endif

#define WOLF_CATEGORY_TEST_LOG		WOLF_LAST_INTERNAL_CATEGORY+1

#define WOLF_MSG_TEST_LOG_BASE		( WOLF_CATEGORY_TEST_LOG ) * 1000
#define WOLF_MSG_TEST_LOG_MSG1		WOLF_MSG_TEST_LOG_BASE+1
#define WOLF_MSG_TEST_LOG_MSG2		WOLF_MSG_TEST_LOG_BASE+2
#define WOLF_MSG_TEST_LOG_MSG3		WOLF_MSG_TEST_LOG_BASE+3
#define WOLF_MSG_TEST_LOG_MSG4		WOLF_MSG_TEST_LOG_BASE+4
#define WOLF_MSG_TEST_LOG_MSG5		WOLF_MSG_TEST_LOG_BASE+5
#define WOLF_MSG_TEST_LOG_MSG6		WOLF_MSG_TEST_LOG_BASE+6
#define WOLF_MSG_TEST_LOG_MSG7		WOLF_MSG_TEST_LOG_BASE+7
#define WOLF_MSG_TEST_LOG_MSG8		WOLF_MSG_TEST_LOG_BASE+8
#define WOLF_MSG_TEST_LOG_MSG9		WOLF_MSG_TEST_LOG_BASE+9

int main( void ) {
	char errbuf[512];
#ifdef _WIN32
	DWORD last_error;
#endif

	wolf_log_openlogtostderr( WOLF_LOG_NOTICE );
	wolf_log_openlogtofile( "test.log", WOLF_LOG_NOTICE );
#if defined WOLF_LOG_HAVE_SYSLOG
	wolf_log_openlogtosyslog( "test", WOLF_LOG_DAEMON, WOLF_LOG_NOTICE,
		WOLF_LOG_SYSLOG_DEFAULT_OPTIONS );
#endif
#if defined WOLF_LOG_HAVE_EVENTLOG
	wolf_log_openlogtoeventlog( NULL, "Application", "wolf_log_test",
		"C:\\Temp\\testmsg.dll",  WOLF_LAST_INTERNAL_CATEGORY+1, WOLF_LOG_NOTICE );
#endif

	/* parameters and levels */

	wolf_log( WOLF_LOG_ERR, WOLF_CATEGORY_TEST_LOG, WOLF_MSG_TEST_LOG_MSG1,
		"This is error %d", 5 );
	wolf_log( WOLF_LOG_WARNING, WOLF_CATEGORY_TEST_LOG, WOLF_MSG_TEST_LOG_MSG2,
		"Warning, this is just a test" );
	wolf_log( WOLF_LOG_NOTICE, WOLF_CATEGORY_TEST_LOG, WOLF_MSG_TEST_LOG_MSG3,
		"Started the logger" );
	wolf_log( WOLF_LOG_DEBUG, WOLF_CATEGORY_TEST_LOG, WOLF_MSG_TEST_LOG_MSG4,
		"This will not appear" );
	wolf_log( WOLF_LOG_NOTICE, WOLF_CATEGORY_TEST_LOG, WOLF_MSG_TEST_LOG_MSG5,
		"Closed the logger" );

	wolf_log( WOLF_LOG_ERR, WOLF_CATEGORY_TEST_LOG, WOLF_MSG_TEST_LOG_MSG6,
		"This is error %d with %s", 5, "foobar" );

	/* different ways of handling system errors (errno, GetLastError) */

	errno = 5;
	(void)wolf_system_error_msg( errbuf, 512 );
	wolf_log( WOLF_LOG_ERR, WOLF_CATEGORY_TEST_LOG, WOLF_MSG_TEST_LOG_MSG7,
		"A Unix system error occured during %s phase: %s (%d)", "fla",
		errbuf, errno );

#ifdef _WIN32
	SetLastError( 18 );
	(void)wolf_system_error_msg( errbuf, 512 );
	wolf_log( WOLF_LOG_ERR, WOLF_CATEGORY_TEST_LOG, WOLF_MSG_TEST_LOG_MSG8,
		"A Windows error occured during %s phase: %s (%d)", "fla",
		errbuf, GetLastError( ) );
#endif

	/* HTTP link to a knowledge base or feedback form, this results in a
	 * HTTP GET request to the address:
	 *
	 * http://wolfbones.andreasbaumann.cc/?EvtSrc=wolf_log_test&EvtCat=Test%20Category%20&EvtID=3009
 	 *
	 * Really good thinking here!
	 */
	wolf_log( WOLF_LOG_ERR, WOLF_CATEGORY_TEST_LOG, WOLF_MSG_TEST_LOG_MSG9,
		"Error %d, see more on http://wolfbones.andreasbaumann.cc",
		66 );

#if defined WOLF_LOG_HAVE_EVENTLOG
	wolf_log_closelogtoeventlog( );
#endif
#if defined WOLF_LOG_HAVE_SYSLOG
	wolf_log_closelogtosyslog( );
#endif
	wolf_log_closelogtofile( );
	wolf_log_closelogtostderr( );

	return EXIT_SUCCESS;
}