/* $Id: logfile.c,v 1.5 2004/03/26 10:17:12 jmuelmen Exp $ */ #include #include #include #include #include #include #include "log_int.h" #include "conf_int.h" #define NF 1024 static struct log_file _files[NF]; static int _nf = 0; /* comparison function used in search and sort */ static int logfile_compar (const void *k, const void *f) { int ret; const struct log_file *f1 = (const struct log_file *)k; const struct log_file *f2 = (const struct log_file *)f; ret = strncmp(f1->modname, f2->modname, STRLEN); if (ret) return ret; ret = strncmp(f1->fname, f2->fname, STRLEN); return ret; } static struct log_file *logfile_search (const char *mod_name, const char *file_name) { struct log_file key; strncpy(key.fname, file_name, STRLEN); strncpy(key.modname, mod_name, STRLEN); return bsearch(&key, _files, _nf, sizeof(struct log_file), logfile_compar); } static void logfile_sort () { qsort(_files, _nf, sizeof(struct log_file), logfile_compar); } static int logfile_init (const char *mod_name, const char *file_name) { int ret; char path[STRLEN]; char fname[STRLEN]; struct stat statbuf; struct log_file f; if (_nf == NF) return -1; strncpy(f.fname, file_name, STRLEN); strncpy(f.modname, mod_name, STRLEN); /* get the path from the config table */ if (conf_get("TURBODAQ_DIR", path, STRLEN)) { fprintf(stderr, "Configuration table doesn't contain TURBODAQ_DIR\n"); return -1; } /* snprintf(path, STRLEN, "/tmp/test"); */ /* check that the directory exists */ if ((ret = stat(path, &statbuf))) { perror("Opening TurboDAQ_files"); return -1; } if (!S_ISDIR(statbuf.st_mode)) { fprintf(stderr, "Opening TurboDAQ_files: not a directory\n"); return -1; } /* check if the module directory is there already; make it otherwise */ snprintf(fname, STRLEN, "%s/%s", path, mod_name); if (ret = stat(fname, &statbuf)) { if (errno == ENOENT) /* this we can deal with */ { if (ret = mkdir(fname, 0777)) { perror("Creating module directory"); return -1; } } else { perror("Opening module directory"); return -1; } } else /* check that fname is really a directory */ { if (!S_ISDIR(statbuf.st_mode)) { fprintf(stderr, "Opening module directory: not a directory\n"); return -1; } } /* check if the module data directory is there already; make it otherwise */ snprintf(fname, STRLEN, "%s/%s/data", path, mod_name); if (ret = stat(fname, &statbuf)) { if (errno == ENOENT) /* this we can deal with */ { if (ret = mkdir(fname, 0777)) { perror("Creating module data directory"); return -1; } } else { perror("Opening module data directory"); return -1; } } else /* check that fname is really a directory */ { if (!S_ISDIR(statbuf.st_mode)) { fprintf(stderr, "Opening module data directory: not a " "directory\n"); return -1; } } /* check if the module ambush data directory is there already; make it otherwise */ snprintf(fname, STRLEN, "%s/%s/data/ambush", path, mod_name); if (ret = stat(fname, &statbuf)) { if (errno == ENOENT) /* this we can deal with */ { if (ret = mkdir(fname, 0777)) { perror("Creating module ambush data directory"); return -1; } } else { perror("Opening module ambush data directory"); return -1; } } else /* check that fname is really a directory */ { if (!S_ISDIR(statbuf.st_mode)) { fprintf(stderr, "Opening module ambush data directory: not a " "directory\n"); return -1; } } /* open the log file */ snprintf(fname, STRLEN, "%s/%s/data/ambush/%s", path, mod_name, file_name); if (!(f.f = fopen(fname, "a+"))) { perror("Opening logfile"); return -1; } /* add to the array of files */ _files[_nf++] = f; /* and sort */ logfile_sort(); return 0; } int logfile_open_file (const char *mod_name, const char *file_name) { if (logfile_search(mod_name, file_name)) return 0; return logfile_init(mod_name, file_name); } FILE *logfile_get_file (const char *mod_name, const char *file_name) { struct log_file *f = logfile_search(mod_name, file_name); /* fprintf(stderr, "looking for file for %s/%s\n", mod_name, file_name); */ if (!f) { fprintf(stderr, "got nothing\n"); return NULL; } /* fprintf(stderr, "got file for %s/%s\n", f->fname, f->modname); */ return f->f; }