示例#1
0
int main(const int argc, const char *argv[])
{
    event_set_log_callback(write_to_file_cb);
    event_enable_debug_mode();

    //log_level(LOG_DEBUG);
    log_fileline(LOG_FILELINE_ON);
    log_level(LOG_WARN);

    server_t *server = server_init(DEFAULT_PORT);

    if(server == NULL)
    {
        log_err(__FILE__, __LINE__, "Cannot init server.");
        exit(-1);
    }

    int power = 10;
    server->service_idx = service_index_init(power);
    char *str = NULL;

    str = calloc(sizeof(char), 5);
    memcpy(str, "aaaa", 4);
    service_t *aaaa = service_init(str);
    aaaa = service_add(server->service_idx, power, aaaa);

    server->service_first = aaaa;
    server->service_last  = aaaa;
    server->num_services++;

    str = calloc(sizeof(char), 5);
    memcpy(str, "bbbb", 4);
    service_t *bbbb = service_init(str);
    bbbb = service_add(server->service_idx, power, bbbb);

    aaaa->all_next = bbbb;
    server->service_last = bbbb;
    server->num_services++;

    str = calloc(sizeof(char), 5);
    memcpy(str, "cccc", 4);
    service_t *cccc = service_init(str);
    cccc = service_add(server->service_idx, power, cccc);

    bbbb->all_next = cccc;
    server->service_last = cccc;
    server->num_services++;

    server_event_run(server);

    return 0;
}
示例#2
0
void _logout(int lvl, const char *file, int line, const char *func, const char *format, ...)
{
	va_list ap, ap_t;
	char *buf = NULL;
	int ret;
	struct timeval tv;

	//printf("UNTHREADED LOG\n");
	if (lvl > log_level) return;

	// Get the time as soon as possible
	gettimeofday(&tv, NULL);

	// Init the log if needed
	init_log();

	// Format output string
	va_start(ap, format);
	va_copy(ap_t, ap);
	ret = vsnprintf(buf, 0, format, ap_t);
	buf = malloc(ret+1);
	ret = vsnprintf(buf, ret+1, format, ap);
	va_end(ap);

	log_lock();
	// TODO: Queue to background thread
	
	// Log the type of the thing to log
	log_type(lvl);
	// Log the timestamp
	log_time(&tv);
	// Output formatted string
	putc(' ', *log_out);
	set_lvlcolor(lvl);
	fputs(buf, *log_out);
	// Log function, file and line number
	log_fileline(func, file, line);
	// Log EOL
	log_lineend();
	// Unlock the thread
	log_unlock();
	
	// Free the buffer
	if (buf) free(buf);
}