Browse Source

Add small test on a logline + '\n' at logline end

Yann Weber 5 years ago
parent
commit
1d4a84b8c7
3 changed files with 23 additions and 9 deletions
  1. 6
    3
      src/logger.c
  2. 1
    1
      tests/Makefile.am
  3. 16
    5
      tests/check_logger.c

+ 6
- 3
src/logger.c View File

567
 
567
 
568
 	if(!fmt->_msg)
568
 	if(!fmt->_msg)
569
 	{ //first message, allocating message buffer
569
 	{ //first message, allocating message buffer
570
-		fmt->_msglen = fmt->buflen[0] + fmt->buflen[1] + fmt_msg_len;
570
+		fmt->_msglen = fmt->buflen[0] + fmt->buflen[1] + fmt_msg_len + 1;
571
 		// rounding upper
571
 		// rounding upper
572
 		fmt->_msglen >>= 6;
572
 		fmt->_msglen >>= 6;
573
 		fmt->_msglen++;
573
 		fmt->_msglen++;
591
 			// TODO check errors
591
 			// TODO check errors
592
 			break;
592
 			break;
593
 		}
593
 		}
594
-		fmt->_msglen = fmt_msg_len + fmt->buflen[0] + fmt->buflen[1];
594
+		fmt->_msglen = fmt_msg_len + fmt->buflen[0] + fmt->buflen[1] + 1;
595
 		fmt->_msglen >>= 6;
595
 		fmt->_msglen >>= 6;
596
 		fmt->_msglen++;
596
 		fmt->_msglen++;
597
 		fmt->_msglen <<= 6;
597
 		fmt->_msglen <<= 6;
610
 		strncpy(fmt->_msg + suff_off, fmt->suffix,
610
 		strncpy(fmt->_msg + suff_off, fmt->suffix,
611
 			fmt->buflen[1]);
611
 			fmt->buflen[1]);
612
 		//TODO check errors
612
 		//TODO check errors
613
+		fmt->_msg[suff_off+fmt->buflen[1]] = '\n';
614
+		fmt->_msg[suff_off+fmt->buflen[1]+1] = '\0';
613
 	}
615
 	}
614
 	else
616
 	else
615
 	{
617
 	{
616
-		fmt->_msg[suff_off] = '\0';
618
+		fmt->_msg[suff_off] = '\n';
619
+		fmt->_msg[suff_off+1] = '\0';
617
 	}
620
 	}
618
 	// update fmt->_msg dynamic fields
621
 	// update fmt->_msg dynamic fields
619
 	time(&_curtime);
622
 	time(&_curtime);

+ 1
- 1
tests/Makefile.am View File

5
 check_logger_LDADD = $(top_builddir)/src/libpyfcgi.a @CHECK_LIBS@
5
 check_logger_LDADD = $(top_builddir)/src/libpyfcgi.a @CHECK_LIBS@
6
 
6
 
7
 clean-local:
7
 clean-local:
8
-	-rm -rf tmp_PyFCGI_checks*
8
+	-rm -rf /tmp/tmp_PyFCGI_checks*

+ 16
- 5
tests/check_logger.c View File

6
 
6
 
7
 pyfcgi_conf_logger_t *conf = &PyFCGI_conf.logs;
7
 pyfcgi_conf_logger_t *conf = &PyFCGI_conf.logs;
8
 
8
 
9
-char tmpdir[] = "./tmp_PyFCGI_checksXXXXXX";
9
+char tmpdir[] = "/tmp/tmp_PyFCGI_checksXXXXXX";
10
 
10
 
11
 /*
11
 /*
12
 	TC logger init
12
 	TC logger init
58
 START_TEST(test_logger_add)
58
 START_TEST(test_logger_add)
59
 {
59
 {
60
 	char tmplog[128];
60
 	char tmplog[128];
61
-	char logfmt[] = "{level} {ident} {msg}";
61
+	char logfmt[] = "{level} {ident} {pid} {msg}";
62
+	char expected[] = "  Alert  0000 Hello world ! foobar 04\n";
62
 	int ret;
63
 	int ret;
63
 	pyfcgi_conf_logger_t *conf;
64
 	pyfcgi_conf_logger_t *conf;
64
 	pyfcgi_logger_t *logger;
65
 	pyfcgi_logger_t *logger;
65
 	pyfcgi_logger_format_t *fmt;
66
 	pyfcgi_logger_format_t *fmt;
67
+	char buf[128];
68
+	size_t sz;
69
+
70
+	memset(buf, 0, 128);
66
 
71
 
67
 	pyfcgi_logger_init();
72
 	pyfcgi_logger_init();
68
 
73
 
77
 	fmt = &(conf->formats[logger->fmt_id]);
82
 	fmt = &(conf->formats[logger->fmt_id]);
78
 	ck_assert_str_eq(fmt->fmt, logfmt);
83
 	ck_assert_str_eq(fmt->fmt, logfmt);
79
 
84
 
80
-	pyfcgi_log(LOG_ALERT, "Hello world ! %s\n", tmplog);
85
+	pyfcgi_log(LOG_ALERT, "Hello world ! %s %02d", "foobar", 4);
86
+	//pyfcgi_log(LOG_DEBUG, "Hello world 2 %s\n", tmplog);
81
 
87
 
82
 	pyfcgi_logger_stop();
88
 	pyfcgi_logger_stop();
83
 
89
 
84
-	//unlink(tmplog);
90
+	ret = open(tmplog, O_RDONLY);
91
+	sz = read(ret, buf, 128);
92
+
93
+	ck_assert_str_eq(expected, buf);
94
+
95
+	unlink(tmplog);
85
 	
96
 	
86
 }
97
 }
87
 END_TEST
98
 END_TEST
582
 	nfailed = srunner_ntests_failed(sr);
593
 	nfailed = srunner_ntests_failed(sr);
583
 	srunner_free(sr);
594
 	srunner_free(sr);
584
 
595
 
585
-	//rmdir(tmpdir);
596
+	rmdir(tmpdir);
586
 
597
 
587
 	return (!nfailed)?EXIT_SUCCESS:EXIT_FAILURE;
598
 	return (!nfailed)?EXIT_SUCCESS:EXIT_FAILURE;
588
 }
599
 }

Loading…
Cancel
Save