SMREmulator 1.0
ShingledMagneticRecordingEmulator
|
00001 00011 #include <stdio.h> 00012 #include <fcntl.h> 00013 #include <errno.h> 00014 #include <stdarg.h> 00015 #include <string.h> 00016 #include <unistd.h> 00017 #include <sys/time.h> 00018 #include <sys/stat.h> 00019 00020 #include <emulog.h> 00021 #include <emudebug.h> 00022 00023 00024 /* -- Macro Definitions -- */ 00025 #define LOG_BUF_SIZE 4096 00026 00027 00028 00029 /* -- Function Prototypes -- */ 00030 static int emulog_error(const char *, ...); 00031 static int emulog_info(const char *, ...); 00032 static int emulog_msg(const char *, const char *, va_list); 00033 00034 00035 00036 /* -- Static Variables -- */ 00037 static edi_lops_t emulog_ops; 00038 static int emulog_fd = 0; 00039 00040 00041 00042 /* -- Function Implementations -- */ 00043 00055 edi_lops_t *emulog_open(const char *log_file) 00056 { 00057 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; 00058 int oflags = O_RDWR | O_CREAT | O_APPEND; 00059 00060 00061 /* Open the log file */ 00062 if ((emulog_fd = open(log_file, oflags, mode)) < 0) 00063 return NULL; 00064 dbg("Log file %s opened. File descriptor is %d\n", log_file, emulog_fd); 00065 00066 /* Return a pointer to the log callback functions structure */ 00067 emulog_ops.error = emulog_error; 00068 emulog_ops.info = emulog_info; 00069 return &emulog_ops; 00070 } 00071 00072 00073 00079 int emulog_close() 00080 { 00081 /* Attempt to close the log file */ 00082 if (close(emulog_fd) < 0) 00083 return -1; 00084 00085 return 0; 00086 } 00087 00088 00089 00098 static int emulog_error(const char *format, ...) 00099 { 00100 va_list ap; 00101 int retval; 00102 00103 00104 /* Read the variable argument list */ 00105 va_start(ap, format); 00106 retval = emulog_msg("ERROR", format, ap); 00107 va_end(ap); 00108 return retval; 00109 } 00110 00111 00112 00121 static int emulog_info(const char *format, ...) 00122 { 00123 va_list ap; 00124 int retval; 00125 00126 00127 /* Read the variable argument list */ 00128 va_start(ap, format); 00129 retval = emulog_msg("INFO ", format, ap); 00130 va_end(ap); 00131 return retval; 00132 } 00133 00134 00135 00147 static int emulog_msg(const char *type, const char *format, va_list ap) 00148 { 00149 char buf[LOG_BUF_SIZE]; 00150 int len; 00151 struct timeval timestamp; 00152 00153 00154 /* Construct the log message */ 00155 len = snprintf(buf, LOG_BUF_SIZE, "%s ", type); 00156 00157 gettimeofday(×tamp, NULL); 00158 len += strftime(&buf[len], LOG_BUF_SIZE - len, "%a %b %d %Y %H:%M:%S", 00159 localtime(×tamp.tv_sec)); 00160 len += snprintf(&buf[len], LOG_BUF_SIZE - len, ".%ld ", timestamp.tv_usec); 00161 00162 if (vsnprintf(&buf[len], LOG_BUF_SIZE - len, format, ap) < 0) { 00163 fprintf(stderr, "Error logging message: "); 00164 vfprintf(stderr, format, ap); 00165 fprintf(stderr, "\n"); 00166 return -1; 00167 } 00168 len = strlen(buf); 00169 00170 00171 /* Output the message to the log file */ 00172 if (write(emulog_fd, buf, len) < len) { 00173 fprintf(stderr, "Error logging message: %s\n", buf); 00174 return -1; 00175 } 00176 00177 return 0; 00178 }