File: test_log.cc

package info (click to toggle)
mpich 4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,184 kB
  • sloc: ansic: 1,040,629; cpp: 82,270; javascript: 40,763; perl: 27,933; python: 16,041; sh: 14,676; xml: 14,418; f90: 12,916; makefile: 9,270; fortran: 8,046; java: 4,635; asm: 324; ruby: 103; awk: 27; lisp: 19; php: 8; sed: 4
file content (379 lines) | stat: -rw-r--r-- 10,793 bytes parent folder | download | duplicates (5)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/**
* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2001-2017. ALL RIGHTS RESERVED.
* See file LICENSE for terms.
*/

#include <common/test.h>
#include <fstream>
#include <set>
#include <dirent.h>

extern "C" {
#include <ucs/debug/log.h>
#include <ucs/sys/compiler.h>
}

class log_test : private ucs::clear_dontcopy_regions, public ucs::test {
public:
    virtual void init() {
#ifdef __SANITIZE_ADDRESS__
        /* BUG: on Ubuntu22.04 if LD_PRELOAD=libasan.so
         * grep returns 1 error code even if pattern was found
         */
        UCS_TEST_SKIP_R("skipping on asan");
#endif
        /* skip because logger does not support file
         * output on valgrind
         */
        if (RUNNING_ON_VALGRIND) {
            UCS_TEST_SKIP_R("skipping on valgrind");
        }

        const char *default_tmp_dir = "/tmp";

        ucs::test::init();

        ucs_log_cleanup();
        push_config();
        const char *tmp_dir = getenv("TMPDIR");
        if (tmp_dir == NULL) {
            tmp_dir = default_tmp_dir;
        }

        tmp_dir_path  = tmp_dir;
        template_name = "gtest_ucs_log." + ucs::to_string(getpid());

        std::string logfile = template_grep_name =
            tmp_dir_path + "/" + template_name;

        /* add date/time to the log file name in order to track how many
         * different log files were created during testing */
        logfile += ".%t";

        /* add `*` to the template grep name to be able searching a test
         * string in all possible file names with the time specified */
        template_grep_name += ".*";

        /* remove already created files with the similar file name */
        log_files_foreach(&log_test::remove_file);

        std::string ucs_log_spec = "file:" + logfile;
        modify_config("LOG_FILE", ucs_log_spec);
        modify_config("LOG_LEVEL", "info");
        ucs_log_init();
    }

    virtual void cleanup() {
        ucs_log_cleanup();
        pop_config();
        check_log_file();
        unsigned files_count = log_files_foreach(&log_test::remove_file);
        EXPECT_LE(files_count, ucs_global_opts.log_file_rotate + 1);
        EXPECT_NE(0, files_count);
        ucs_log_init();
        ucs::test::cleanup();
    }

    void remove_file(const std::string &name, void *arg) {
        unlink(name.c_str());
    }

    typedef void (log_test::*log_file_foreach_cb)(const std::string &name,
                                                  void *arg);

    unsigned log_files_foreach(log_file_foreach_cb cb, void *arg = NULL) {
        DIR *dir = opendir(tmp_dir_path.c_str());
        struct dirent *entry;
        unsigned files_count = 0;

        while ((entry = readdir(dir)) != NULL) {
            if (strstr(entry->d_name, template_name.c_str()) != NULL) {
                std::string full_file_name = tmp_dir_path + "/" +
                                             ucs::to_string(entry->d_name);
                (this->*cb)(full_file_name, arg);
                files_count++;
            }
        }
        closedir(dir);

        return files_count;
    }

    void test_file_cur_size(const std::string &log_file_name, void *arg) {
        FILE *logfile_fp = fopen(log_file_name.c_str(), "r");
        ASSERT_TRUE(logfile_fp != NULL);

        ucs_log_flush();

        int ret = fseek(logfile_fp, 0, SEEK_END);
        EXPECT_EQ(0, ret);

        long cur_size = ftell(logfile_fp);
        EXPECT_LE(static_cast<size_t>(cur_size), ucs_global_opts.log_file_size);

        fclose(logfile_fp);

        m_log_files_set.insert(log_file_name);
    }

    virtual void check_log_file() {
        ADD_FAILURE() << read_logfile();
    }

    bool do_grep(const std::string &needle) {
        unsigned num_retries       = 0;
        std::string cmd_str        = "<none>";
        std::string system_ret_str = "<none>";

        while (num_retries++ < GREP_RETRIES) {
            /* if this is the last retry, allow printing the grep output */
            std::string grep_cmd = ucs_likely(num_retries != GREP_RETRIES) ?
                                   "grep -q" : "grep";
            cmd_str = grep_cmd + " '" + needle + "' " + template_grep_name;
            int ret = system(cmd_str.c_str());
            if (ret == 0) {
                return true;
            } else if (ret == -1) {
                system_ret_str = ucs::to_string(errno);
            } else {
                system_ret_str = ucs::exit_status_info(ret);
            }

            ucs_log_flush();
        }

        UCS_TEST_MESSAGE << "\"" << cmd_str << "\" failed after "
                         << num_retries - 1 << " iterations ("
                         << system_ret_str << ")";

        return false;
    }

    void read_logfile(const std::string &log_file_name, void *arg) {
        std::stringstream *ss = (std::stringstream*)arg;
        std::ifstream ifs(log_file_name.c_str());
        *ss << log_file_name << ":" << std::endl << ifs.rdbuf() << std::endl;
    }

    std::string read_logfile() {
        std::stringstream ss;
        log_files_foreach(&log_test::read_logfile, &ss);
        return ss.str();
    }

protected:
    std::string template_name;
    std::string template_grep_name;
    std::string tmp_dir_path;
    std::set<std::string> m_log_files_set;

    static const unsigned GREP_RETRIES = 20;
};

class log_test_info : public log_test {
protected:
    log_test_info() :
        m_spacer("  "), m_log_str("hello world"), m_exp_found(true)
    {
    }

    virtual void check_log_file()
    {
        std::string log_str = "UCX  INFO" + m_spacer + m_log_str;
        if (m_exp_found != do_grep(log_str)) {
            ADD_FAILURE() << read_logfile() << " Expected to "
                          << (m_exp_found ? "" : "not ") << "find " << log_str;
        }
    }

    void log_info()
    {
        ucs_info("%s", m_log_str.c_str());
    }

    std::string m_spacer;
    std::string m_log_str;
    bool m_exp_found;
};

UCS_TEST_F(log_test_info, hello) {
    log_info();
}

UCS_TEST_F(log_test_info, hello_indent) {
    ucs_log_indent(1);
    log_info();
    ucs_log_indent(-1);
    m_spacer += "  ";
}

UCS_TEST_F(log_test_info, filter_on, "LOG_FILE_FILTER=*test_lo*.cc") {
    log_info();
}

UCS_TEST_F(log_test_info, filter_off, "LOG_FILE_FILTER=file.c") {
    log_info();
    m_exp_found = false;
}

class log_test_print : public log_test {
    virtual void check_log_file() {
        if (!do_grep("UCX  PRINT debug message")) {
            if (ucs_global_opts.log_print_enable) {
                /* not found but it should be there */
                ADD_FAILURE() << read_logfile();
            }
        } else {
            if (!ucs_global_opts.log_print_enable) {
                /* found but prints disabled!!! */
                ADD_FAILURE() << read_logfile();
            }
        }
    }
};

UCS_TEST_F(log_test_print, print_on, "LOG_PRINT_ENABLE=y") {
    ucs_print("debug message");
}

UCS_TEST_F(log_test_print, print_off) {
    ucs_print("debug message");
}


class log_test_file_size : public log_test {
protected:
    virtual void check_log_file() {
        unsigned files_count = log_files_foreach(&log_test_file_size::
                                                 test_file_cur_size);
        EXPECT_LE(files_count, ucs_global_opts.log_file_rotate + 1);
    }

    virtual void check_log_file(const std::string &test_str) {
        check_log_file();
        EXPECT_TRUE(do_grep(test_str));
    }

    void generate_random_str(std::string &s, size_t len) {
        static const char possible_vals[] =
            "0123456789"
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            "abcdefghijklmnopqrstuvwxyz";

        ASSERT_TRUE(len != 0);

        s.resize(len);

        for (size_t i = 0; i < len; ++i) {
            s[i] = possible_vals[ucs::rand() %
                                 (ucs_static_array_size(possible_vals) - 1)];
        }
    }

    void print_random_str() {
        size_t entry_size = ucs::rand() % ucs_log_get_buffer_size();
        if (entry_size == 0) {
            entry_size = 1;
        }

        std::string entry_buf;

        generate_random_str(entry_buf, entry_size);
        /* use %s here in order to satisfy the "format-security" compilation
         * flag requirements */
        ucs_info("%s", entry_buf.c_str());

        /* to not waste a lot of time grepping the test string */
        if (entry_size < 128) {
            check_log_file(entry_buf);
        } else {
            check_log_file();
        }
    }

    void test_log_file_max_size() {
        const unsigned num_iters = 4;

        for (unsigned i = 0; i < num_iters; i++) {
            size_t set_size, exp_files_count;

            do {
                print_random_str();

                set_size        = m_log_files_set.size();
                exp_files_count = (ucs_global_opts.log_file_rotate + 1);
            } while ((set_size == 0) ||
                     ((set_size % exp_files_count) != 0));
        }

        EXPECT_EQ(m_log_files_set.size(),
                  ucs_global_opts.log_file_rotate + 1);
    }
};

const std::string small_file_size = ucs::to_string(UCS_ALLOCA_MAX_SIZE);

UCS_TEST_F(log_test_file_size, small_file, "LOG_FILE_SIZE=" +
                                           small_file_size) {
    test_log_file_max_size();
}

UCS_TEST_F(log_test_file_size, large_file, "LOG_FILE_SIZE=8k") {
    test_log_file_max_size();
}

UCS_TEST_F(log_test_file_size, small_files, "LOG_FILE_SIZE=" +
                                            small_file_size,
                                            "LOG_FILE_ROTATE=4") {
    test_log_file_max_size();
}

UCS_TEST_F(log_test_file_size, large_files, "LOG_FILE_SIZE=8k",
                                            "LOG_FILE_ROTATE=4") {
    test_log_file_max_size();
}


class log_test_backtrace : public log_test {
    virtual void check_log_file() {
        if (!do_grep("print_backtrace")) {
            ADD_FAILURE() << read_logfile();
        }

#ifdef HAVE_DETAILED_BACKTRACE
        if (!do_grep("main")) {
            ADD_FAILURE() << read_logfile();
        }
#endif
    }
};

UCS_TEST_F(log_test_backtrace, backtrace) {
    ucs_log_print_backtrace(UCS_LOG_LEVEL_INFO);
}

class log_demo : public ucs::test {
};

UCS_MT_TEST_F(log_demo, indent, 4)
{
    ucs::scoped_log_level enable_debug(UCS_LOG_LEVEL_DEBUG);

    ucs_debug("scope begin");

    ucs_log_indent(1);
    EXPECT_EQ(1, ucs_log_get_current_indent());
    ucs_debug("nested log 1");

    ucs_log_indent(1);
    EXPECT_EQ(2, ucs_log_get_current_indent());
    ucs_debug("nested log 1.1");
    ucs_log_indent(-1);

    EXPECT_EQ(1, ucs_log_get_current_indent());
    ucs_debug("nested log 2");
    ucs_log_indent(-1);

    EXPECT_EQ(0, ucs_log_get_current_indent());
    ucs_debug("done");
}