1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
/*
Task Spooler - a task queue system for the unix user
Copyright (C) 2007-2009 LluĂs Batlle i Rossell
Please find the license in the provided COPYING file.
*/
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/time.h>
#include "main.h"
enum Etype
{
WARNING,
ERROR
};
/* Declared in main.h as extern */
enum Process_type process_type;
static int real_errno;
/* Local protos */
static void dump_structs(FILE *out);
static void print_date(FILE *out)
{
time_t t;
const char *tstr;
t = time(NULL);
tstr = ctime(&t);
fprintf(out, "date %s", tstr);
}
static void dump_proc_info(FILE *out)
{
print_date(out);
fprintf(out, "pid %i\n", getpid());
if (process_type == SERVER)
fprintf(out, "type SERVER\n");
else if (process_type == CLIENT)
fprintf(out, "type CLIENT\n");
else
fprintf(out, "type UNKNOWN\n");
}
static FILE * open_error_file()
{
int fd;
FILE* out;
char *path;
int new_size;
char *new_path;
create_socket_path(&path);
new_size = strlen(path)+10;
new_path = malloc(new_size);
strncpy(new_path, path, new_size);
strncat(new_path, ".error", (new_size - strlen(path)) - 1);
free(path);
fd = open(new_path, O_CREAT | O_APPEND | O_WRONLY, 0600);
if (fd == -1)
{
free(new_path);
return 0;
}
out = fdopen(fd, "a");
if (out == NULL)
{
close(fd);
free(new_path);
return 0;
}
free(new_path);
return out;
}
static void print_error(FILE *out, enum Etype type, const char *str, va_list ap)
{
fprintf(out, "-------------------");
if (type == ERROR)
fprintf(out, "Error\n");
else if (type == WARNING)
fprintf(out, "Warning\n");
else
fprintf(out, "Unknown kind of error\n");
fprintf(out, " Msg: ");
vfprintf(out, str, ap);
fprintf(out, "\n");
fprintf(out, " errno %i, \"%s\"\n", real_errno, strerror(real_errno));
}
static void problem(enum Etype type, const char *str, va_list ap)
{
FILE *out;
out = open_error_file();
if (out == 0)
return;
/* out is ready */
print_error(out, type, str, ap);
dump_structs(out);
/* this will close the fd also */
fclose(out);
}
static void problem_msg(enum Etype type, const struct msg *m, const char *str, va_list ap)
{
FILE *out;
out = open_error_file();
if (out == 0)
return;
/* out is ready */
print_error(out, type, str, ap);
msgdump(out, m);
dump_structs(out);
/* this will close the fd also */
fclose(out);
}
void error(const char *str, ...)
{
va_list ap;
va_start(ap, str);
real_errno = errno;
if (process_type == CLIENT)
{
vfprintf(stderr, str, ap);
fputc('\n', stderr);
}
problem(ERROR, str, ap);
exit(-1);
}
void error_msg(const struct msg *m, const char *str, ...)
{
va_list ap;
va_start(ap, str);
real_errno = errno;
problem_msg(ERROR, m, str, ap);
exit(-1);
}
void warning(const char *str, ...)
{
va_list ap;
va_start(ap, str);
real_errno = errno;
problem(WARNING, str, ap);
}
void warning_msg(const struct msg *m, const char *str, ...)
{
va_list ap;
va_start(ap, str);
real_errno = errno;
problem_msg(WARNING, m, str, ap);
}
static void dump_structs(FILE *out)
{
dump_proc_info(out);
if (process_type == SERVER)
{
dump_jobs_struct(out);
dump_notifies_struct(out);
dump_conns_struct(out);
}
}
|