FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > CRUX > CRUX

 
 
LinkBack Thread Tools
 
Old 08-17-2010, 10:46 AM
Ales Kozumplik
 
Default logging: turn the loglevels into proper enum.

This makes the loglevel_to_str array smaller and the logging functions
safer to use.
---
pyanaconda/isys/log.c | 18 +++++++++---------
pyanaconda/isys/log.h | 22 ++++++++++++----------
2 files changed, 21 insertions(+), 19 deletions(-)

diff --git a/pyanaconda/isys/log.c b/pyanaconda/isys/log.c
index c0150bf..e036524 100644
--- a/pyanaconda/isys/log.c
+++ b/pyanaconda/isys/log.c
@@ -37,13 +37,13 @@
static FILE * main_log_tty = NULL;
static FILE * main_log_file = NULL;
static FILE * program_log_file = NULL;
-static int minLevel = INFO;
+static enum loglevel_t minLevel = INFO;
static const char * main_tag = "loader";
static const char * program_tag = "program";
static const int syslog_facility = LOG_LOCAL1;

/* maps our loglevel to syslog loglevel */
-static int mapLogLevel(int level)
+static int mapLogLevel(enum loglevel_t level)
{
switch (level) {
case DEBUGLVL:
@@ -70,7 +70,7 @@ const char *log_level_to_str[] = {
[CRITICAL] = "CRIT"
};

-static void printLogHeader(int level, const char *tag, FILE *outfile) {
+static void printLogHeader(enum loglevel_t level, const char *tag, FILE *outfile) {
struct timeval current_time;
struct tm *t;
int msecs;
@@ -84,7 +84,7 @@ static void printLogHeader(int level, const char *tag, FILE *outfile) {
t->tm_min, t->tm_sec, msecs, level_name, tag);
}

-static void printLogMessage(int level, const char *tag, FILE *outfile, const char *s, va_list ap)
+static void printLogMessage(enum loglevel_t level, const char *tag, FILE *outfile, const char *s, va_list ap)
{
printLogHeader(level, tag, outfile);

@@ -103,7 +103,7 @@ static void retagSyslog(const char* new_tag)
openlog(new_tag, 0, syslog_facility);
}

-void logMessageV(enum logger_t logger, int level, const char * s, va_list ap) {
+void logMessageV(enum logger_t logger, enum loglevel_t level, const char * s, va_list ap) {
FILE *log_tty = main_log_tty;
FILE *log_file = main_log_file;
const char *tag = main_tag;
@@ -137,7 +137,7 @@ void logMessageV(enum logger_t logger, int level, const char * s, va_list ap) {
retagSyslog(main_tag);
}

-void logMessage(int level, const char * s, ...) {
+void logMessage(enum loglevel_t level, const char * s, ...) {
va_list args;

va_start(args, s);
@@ -145,7 +145,7 @@ void logMessage(int level, const char * s, ...) {
va_end(args);
}

-void logProgramMessage(int level, const char * s, ...) {
+void logProgramMessage(enum loglevel_t level, const char * s, ...) {
va_list args;

va_start(args, s);
@@ -200,11 +200,11 @@ void closeLog(void) {
}

/* set the level. higher means you see more verbosity */
-void setLogLevel(int level) {
+void setLogLevel(enum loglevel_t level) {
minLevel = level;
}

-int getLogLevel(void) {
+enum loglevel_t getLogLevel(void) {
return minLevel;
}

diff --git a/pyanaconda/isys/log.h b/pyanaconda/isys/log.h
index 88d0010..3df6cb0 100644
--- a/pyanaconda/isys/log.h
+++ b/pyanaconda/isys/log.h
@@ -23,27 +23,29 @@
#include <stdio.h>
#include <stdarg.h>

-#define DEBUGLVL 10
-#define INFO 20
-#define WARNING 30
-#define ERROR 40
-#define CRITICAL 50
+enum loglevel_t {
+ DEBUGLVL,
+ INFO,
+ WARNING,
+ ERROR,
+ CRITICAL
+};

enum logger_t {
MAIN_LOG = 1,
PROGRAM_LOG = 2
};

-void logMessageV(enum logger_t logger, int level, const char * s, va_list ap)
+void logMessageV(enum logger_t logger, enum loglevel_t level, const char * s, va_list ap)
__attribute__ ((format (printf, 3, 0)));
-void logMessage(int level, const char * s, ...)
+void logMessage(enum loglevel_t level, const char * s, ...)
__attribute__ ((format (printf, 2, 3)));
-void logProgramMessage(int level, const char * s, ...)
+void logProgramMessage(enum loglevel_t level, const char * s, ...)
__attribute__ ((format (printf, 2, 3)));
void openLog();
void closeLog(void);
-void setLogLevel(int minLevel);
-int getLogLevel(void);
+void setLogLevel(enum loglevel_t minLevel);
+enum loglevel_t getLogLevel(void);
int loggingReady(void);

extern int tty_logfd;
--
1.7.1.1

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@redhat.com
https://www.redhat.com/mailman/listinfo/anaconda-devel-list
 
Old 08-17-2010, 08:59 PM
Peter Jones
 
Default logging: turn the loglevels into proper enum.

-#define DEBUGLVL 10
-#define INFO 20
-#define WARNING 30
-#define ERROR 40
-#define CRITICAL 50
+enum loglevel_t {
+ DEBUGLVL,
+ INFO,
+ WARNING,
+ ERROR,
+ CRITICAL
+};


I realize you were probably just following the (bad) example below, but
you should really do this as:

typedef enum {
...
} loglevel_t;

and then all those callers above can be "loglevel_t level" instead of
"enum loglevel_t level". Also naming things _t without them being
typedef's is a sin.


enum logger_t {
MAIN_LOG = 1,
PROGRAM_LOG = 2
};


--
Peter

Space, is big. Really big. You just won't believe how vastly hugely
mindbogglingly big it is. I mean you may think it's a long way down the
road to the chemist, but that's just peanuts to space.
-- The Hitchhiker's Guide to the Galaxy

01234567890123456789012345678901234567890123456789 012345678901234567890123456789

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@redhat.com
https://www.redhat.com/mailman/listinfo/anaconda-devel-list
 

Thread Tools




All times are GMT. The time now is 08:50 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org