Browse Source

log: Add faux_log_facility_str() and tests

Serj Kalichev 3 years ago
parent
commit
0afa9defac
5 changed files with 74 additions and 4 deletions
  1. 4 1
      faux/log.h
  2. 4 0
      faux/log/Makefile.am
  3. 21 1
      faux/log/log.c
  4. 39 0
      faux/log/testc_log.c
  5. 6 2
      faux/testc_module/testc_module.c

+ 4 - 1
faux/log.h

@@ -5,11 +5,14 @@
 #ifndef _faux_log_h
 #define _faux_log_h
 
+#include <syslog.h>
+
 #include "faux/faux.h"
 
 C_DECL_BEGIN
 
-int faux_log_facility(const char *str, int *facility);
+int faux_log_facility_id(const char *str, int *facility);
+const char *faux_log_facility_str(int facility_id);
 
 C_DECL_END
 

+ 4 - 0
faux/log/Makefile.am

@@ -1,2 +1,6 @@
 libfaux_la_SOURCES += \
 	faux/log/log.c
+
+if TESTC
+libfaux_la_SOURCES += faux/log/testc_log.c
+endif

+ 21 - 1
faux/log/log.c

@@ -54,7 +54,7 @@ static struct log_name log_names[] = {
  * @param [out] facility Facility in digital form.
  * @returns 0 - success, < 0 - parsing error
  */
-int faux_log_facility(const char *str, int *facility)
+int faux_log_facility_id(const char *str, int *facility)
 {
 	int i = 0;
 
@@ -72,3 +72,23 @@ int faux_log_facility(const char *str, int *facility)
 
 	return -1;
 }
+
+/** @brief Returns syslog facility string by facility id.
+ *
+ * @param [in] id Facility id.
+ * @return Static facility string or NULL on error.
+ */
+const char *faux_log_facility_str(int facility_id)
+{
+	const char *res = NULL;
+	int i = 0;
+
+	for (i = 0; log_names[i].name; i++) {
+		if (log_names[i].facility == facility_id) {
+			res = log_names[i].name;
+			break;
+		}
+	}
+
+	return res;
+}

+ 39 - 0
faux/log/testc_log.c

@@ -0,0 +1,39 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "faux/log.h"
+#include "faux/str.h"
+
+int testc_faux_log_facility_id(void)
+{
+	const char *str = "daemon";
+	int e = LOG_DAEMON;
+	int r = 0;
+
+	if (faux_log_facility_id(str, &r) < 0) {
+		printf("Can't get id by string\n");
+		return -1;
+	}
+	if (r != e) {
+		printf("Etalon: %d, Result: %d\n", e, r);
+		return -1;
+	}
+
+	return 0;
+}
+
+int testc_faux_log_facility_str(void)
+{
+	int id = LOG_KERN;
+	const char *e = "kern";
+	const char *r = NULL;
+
+	r = faux_log_facility_str(id);
+	if (strcmp(r, e) != 0) {
+		printf("Etalon: %s, Result: %s\n", e, r);
+		return -1;
+	}
+
+	return 0;
+}

+ 6 - 2
faux/testc_module/testc_module.c

@@ -10,10 +10,10 @@ const char *testc_module[][2] = {
 //	{"testc_faux_ini_signal", "Interrupted by signal"}, // demo
 //	{"testc_faux_ini_good", "INI subsystem good"}, // demo
 
-	// Str
+	// str
 	{"testc_faux_str_nextword", "Find next word (quotation)"},
 
-	// INI
+	// ini
 	{"testc_faux_ini_parse_file", "Complex test of INI file parsing"},
 
 	// argv
@@ -25,6 +25,10 @@ const char *testc_module[][2] = {
 	{"testc_faux_timespec_diff", "Diff beetween timespec structures"},
 	{"testc_faux_timespec_sum", "Sum of timespec structures"},
 
+	// log
+	{"testc_faux_log_facility_id", "Converts syslog facility string to id"},
+	{"testc_faux_log_facility_str", "Converts syslog facility id to string"},
+
 	// End of list
 	{NULL, NULL}
 	};