summaryrefslogtreecommitdiffstats
path: root/tests/basic/logchecks.c
blob: a40278b0d7727df8c1c0aa4943c1625787839822 (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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * Copyright (c) 2013 Red Hat, Inc. <http://www.redhat.com>
 * This file is part of GlusterFS.
 *
 * This file is licensed to you under your choice of the GNU Lesser
 * General Public License, version 3 or any later version (LGPLv3 or
 * later), or the GNU General Public License, version 2 (GPLv2), in all
 * cases as published by the Free Software Foundation.
 */

#include <stdio.h>
#include <unistd.h>

#include "glusterfs.h"
#include "globals.h"
#include "logging.h"

#include "logchecks-messages.h"
#include "../../libglusterfs/src/logging.h"

glusterfs_ctx_t   *ctx = NULL;

#define TEST_FILENAME           "/tmp/logchecks.log"
#define GF_LOG_CONTROL_FILE     "/etc/glusterfs/logger.conf"

int
go_log_vargs(gf_loglevel_t level, const char *fmt, ...)
{
        va_list  ap;

        va_start (ap, fmt);
        gf_msg_vplain (level, fmt, ap);
        va_end (ap);

        return 0;
}

int
go_log (void)
{
        /*** gf_msg ***/
        gf_msg ("logchecks", GF_LOG_INFO, 0, logchecks_msg_1);
        gf_msg ("logchecks", GF_LOG_INFO, 22, logchecks_msg_2, 42, "Forty-Two",
                42);
        /* change criticality */
        gf_msg ("logchecks", GF_LOG_CRITICAL, 0, logchecks_msg_3);
        gf_msg ("logchecks", GF_LOG_CRITICAL, 22, logchecks_msg_4, 42,
                "Forty-Two", 42);

        /*** msg_nomem ***/
        gf_msg_nomem ("logchecks", GF_LOG_ALERT, 555);
        gf_msg_nomem ("logchecks", GF_LOG_INFO, 555);

        /*** msg_plain ***/
        gf_msg_plain (GF_LOG_INFO, "Informational: gf_msg_plain with"
                        " args %d:%s:%x", 42, "Forty-Two", 42);
        gf_msg_plain (GF_LOG_ALERT, "Alert: gf_msg_plain with"
                        " args %d:%s:%x", 42, "Forty-Two", 42);

        /*** msg_vplain ***/
        go_log_vargs (GF_LOG_INFO, "Informational: gf_msg_vplain: No args!!!");
        go_log_vargs (GF_LOG_INFO, "Informational: gf_msg_vplain: Some"
                        " args %d:%s:%x", 42, "Forty-Two", 42);
        go_log_vargs (GF_LOG_INFO, "Critical: gf_msg_vplain: No args!!!");
        go_log_vargs (GF_LOG_INFO, "Critical: gf_msg_vplain: Some"
                        " args %d:%s:%x", 42, "Forty-Two", 42);

        /*** msg_plain_nomem ***/
        gf_msg_plain_nomem (GF_LOG_INFO, "Informational: gf_msg_plain_nomem");
        gf_msg_plain_nomem (GF_LOG_ALERT, "Alert: gf_msg_plain_nomem");

        /*** msg_backtrace_nomem ***/
        // TODO: Need to create a stack depth and then call
        gf_msg_backtrace_nomem (GF_LOG_INFO, 5);
        gf_msg_backtrace_nomem (GF_LOG_ALERT, 5);

        /*** gf_msg_callingfn ***/
        // TODO: Need to create a stack depth and then call
        gf_msg_callingfn ("logchecks", GF_LOG_INFO, 0, logchecks_msg_7);
        gf_msg_callingfn ("logchecks", GF_LOG_INFO, 0, logchecks_msg_8, 42,
                          "Forty-Two", 42);
        gf_msg_callingfn ("logchecks", GF_LOG_CRITICAL, 0, logchecks_msg_9);
        gf_msg_callingfn ("logchecks", GF_LOG_CRITICAL, 0, logchecks_msg_10, 42,
                          "Forty-Two", 42);

        /*** gf_msg_debug ***/
        gf_msg_debug ("logchecks", 0, "Debug: Hello World!!!");
        gf_msg_debug ("logchecks", 22, "Debug: With args %d:%s:%x", 42,
                      "Forty-Two", 42);

        /*** gf_msg_trace ***/
        gf_msg_trace ("logchecks", 0, "Trace: Hello World!!!");
        gf_msg_trace ("logchecks", 22, "Trace: With args %d:%s:%x", 42,
                      "Forty-Two", 42);

        /*** gf_msg_backtrace ***/
        // TODO: Test with lower callstr values to check truncation

        return 0;
}

int
main (int argc, char *argv[])
{
        int                ret = -1;

        unlink (GF_LOG_CONTROL_FILE);
        creat (GF_LOG_CONTROL_FILE, O_RDONLY);
        ctx = glusterfs_ctx_new ();
        if (!ctx)
                return -1;

        ret = glusterfs_globals_init (ctx);
        if (ret) {
                printf ("Error from glusterfs_globals_init [%s]\n",
                        strerror (errno));
                return ret;
        }

        /* Pre init test, message should not be printed */
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_19);

        THIS->ctx = ctx;

        /* TEST 1: messages before initializing the log, goes to stderr
         * and syslog based on criticality */
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_11);
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_12);
        go_log ();
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_11);

        /* TEST 2: messages post initialization, goes to glusterlog and
         * syslog based on severity */
        ret = gf_log_init(ctx, TEST_FILENAME, "logchecks");
        if (ret != 0) {
                printf ("Error from gf_log_init [%s]\n", strerror (errno));
                return -1;
        }
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_11);
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_13);
        go_log ();
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_11);

        /* TEST 3: Test rotation */
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_11);
        gf_log_logrotate (0);
        gf_msg ("logchecks", GF_LOG_CRITICAL, 0, logchecks_msg_5);
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_11);

        /* TEST 4: Check flush, nothing noticeable should occur :) */
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_11);
        gf_log_flush ();
        gf_msg ("logchecks", GF_LOG_CRITICAL, 0, logchecks_msg_6);
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_11);

        /* TEST 5: Change format */
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_11);
        gf_log_set_logformat (gf_logformat_traditional);
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_14);
        go_log ();
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_11);

        /* TEST 6: Change level */
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_11);
        gf_log_set_loglevel (ctx, GF_LOG_CRITICAL);
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_15);
        go_log ();
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_11);

        /* Reset to run with syslog */
        gf_log_set_logformat (gf_logformat_withmsgid);
        gf_log_set_loglevel (ctx, GF_LOG_INFO);

        /* Run tests with logger changed to syslog */
        /* TEST 7: No more gluster logs */
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_11);
        gf_log_set_logger (gf_logger_syslog);
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_16);
        go_log ();
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_11);

        /* TEST 8: Change format */
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_11);
        gf_log_set_logformat (gf_logformat_traditional);
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_14);
        go_log ();
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_11);

        /* TEST 9: Change level */
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_11);
        gf_log_set_loglevel (ctx, GF_LOG_CRITICAL);
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_15);
        go_log ();
        gf_msg ("logchecks", GF_LOG_ALERT, 0, logchecks_msg_11);

        // TODO: signal crash prints, but not yet feasible here
        // TODO: Graph printing
        // TODO: Multi threaded logging

        /* Close out the logging */
        gf_log_fini (ctx);
        gf_log_globals_fini ();

        unlink (GF_LOG_CONTROL_FILE);
        unlink (TEST_FILENAME);

        return 0;
}