Skip to content

Commit

Permalink
Added SwooleG.log_level and swoole_error_log function.
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Feb 25, 2016
1 parent 5f77c3e commit 496d547
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 52 deletions.
9 changes: 1 addition & 8 deletions include/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ enum swEventType
SW_EVENT_PROXY_END = 17,
};

#define SW_HOST_MAXSIZE 128
#define SW_MAX_TMP_PKG 1000
#define SW_LOG_FILENAME 128

enum swIPCMode
{
SW_IPC_UNSOCK = 1,
Expand Down Expand Up @@ -330,9 +326,6 @@ struct _swServer
int sock_client_buffer_size; //client的socket缓存区设置
int sock_server_buffer_size; //server的socket缓存区设置

int log_level;
char log_file[SW_LOG_FILENAME];

int signal_fd;
int event_fd;

Expand Down Expand Up @@ -767,7 +760,7 @@ static sw_inline swConnection *swServer_connection_verify(swServer *serv, int se
#ifdef SW_USE_OPENSSL
if (conn->ssl && conn->ssl_state != SW_SSL_STATE_READY)
{
swWarn("SSL not ready");
swoole_error_log(SW_LOG_NOTICE, SW_ERROR_SSL_NOT_READY, "SSL not ready");
return NULL;
}
#endif
Expand Down
16 changes: 11 additions & 5 deletions include/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,23 @@

enum swErrorCode
{
SW_ERROR_MALLOC_FAIL = 501,
SW_ERROR_SYSTEM_CALL_FAIL = 502,
/**
* reactor thread
* master process
*/
SW_ERROR_SERVER_MUST_CREATED_BEFORE_CLIENT = 9001,
SW_ERROR_SERVER_TOO_MANY_SOCKET = 9002,
/**
* server event worker
* connection error
*/
SW_ERROR_SERVER_WORKER_CLOSING = 8001,
SW_ERROR_SESSION_CLOSED_BY_SERVER = 1001,
SW_ERROR_SESSION_CLOSED_BY_CLIENT = 1002,
SW_ERROR_OUTPUT_BUFFER_OVERFLOW = 1003,
SW_ERROR_SESSION_CLOSED_BY_CLIENT,
SW_ERROR_SESSION_CLOSING,
SW_ERROR_SESSION_CLOSED,
SW_ERROR_SESSION_NO_EXIST,
SW_ERROR_OUTPUT_BUFFER_OVERFLOW,
SW_ERROR_SSL_NOT_READY,
};

#endif /* SW_ERRNO_H_ */
18 changes: 11 additions & 7 deletions include/swoole.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ enum swLog_level
SW_LOG_TRACE,
SW_LOG_INFO,
SW_LOG_NOTICE,
SW_LOG_WARN,
SW_LOG_WARNING,
SW_LOG_ERROR,

};
Expand All @@ -260,7 +260,7 @@ enum swWorker_status

#define swWarn(str,...) SwooleGS->lock.lock(&SwooleGS->lock);\
snprintf(sw_error,SW_ERROR_MSG_SIZE,"%s: "str,__func__,##__VA_ARGS__);\
swLog_put(SW_LOG_WARN, sw_error);\
swLog_put(SW_LOG_WARNING, sw_error);\
SwooleGS->lock.unlock(&SwooleGS->lock)

#define swNotice(str,...) SwooleGS->lock.lock(&SwooleGS->lock);\
Expand All @@ -276,14 +276,15 @@ exit(1)

#define swSysError(str,...) SwooleGS->lock.lock(&SwooleGS->lock);\
snprintf(sw_error,SW_ERROR_MSG_SIZE,"%s(:%d): "str" Error: %s[%d].",__func__,__LINE__,##__VA_ARGS__,strerror(errno),errno);\
swLog_put(SW_LOG_WARN, sw_error);\
SwooleGS->lock.unlock(&SwooleGS->lock)

#define swRuntimeError(error,str,...) SwooleGS->lock.lock(&SwooleGS->lock);\
snprintf(sw_error,SW_ERROR_MSG_SIZE,"(ERROR %d): "str,error,##__VA_ARGS__);\
swLog_put(SW_LOG_ERROR, sw_error);\
SwooleGS->lock.unlock(&SwooleGS->lock)

#define swoole_error_log(level, errno, str, ...) if (level >= SwooleG.log_level){\
snprintf(sw_error, SW_ERROR_MSG_SIZE, "(ERROR %d): "str,errno,##__VA_ARGS__);\
SwooleGS->lock.lock(&SwooleGS->lock);\
swLog_put( SW_LOG_ERROR, sw_error);\
SwooleGS->lock.unlock(&SwooleGS->lock);}

#ifdef SW_DEBUG_REMOTE_OPEN
#define swDebug(str,...) int __debug_log_n = snprintf(sw_error,SW_ERROR_MSG_SIZE,str,##__VA_ARGS__);\
write(SwooleG.debug_fd, sw_error, __debug_log_n);
Expand Down Expand Up @@ -1716,6 +1717,9 @@ typedef struct
char *user;
char *group;

uint8_t log_level;
char *log_file;

/**
* task worker process num
*/
Expand Down
15 changes: 5 additions & 10 deletions src/core/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ void swLog_put(int level, char *cnt)
level_str = "NOTICE";
break;
case SW_LOG_ERROR:
level_str = "ERR";
level_str = "ERROR";
break;
case SW_LOG_WARN:
level_str = "WARN";
case SW_LOG_WARNING:
level_str = "WARNING";
break;
case SW_LOG_TRACE:
level_str = "TRACE";
Expand All @@ -71,8 +71,7 @@ void swLog_put(int level, char *cnt)
struct tm *p;
t = time(NULL);
p = localtime(&t);
snprintf(date_str, SW_LOG_DATE_STRLEN, "%d-%02d-%02d %02d:%02d:%02d", p->tm_year + 1900, p->tm_mon + 1, p->tm_mday,
p->tm_hour, p->tm_min, p->tm_sec);
snprintf(date_str, SW_LOG_DATE_STRLEN, "%d-%02d-%02d %02d:%02d:%02d", p->tm_year + 1900, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);

char process_flag = '@';
int process_id = 0;
Expand All @@ -99,9 +98,5 @@ void swLog_put(int level, char *cnt)
}

n = snprintf(log_str, SW_LOG_BUFFER_SIZE, "[%s %c%d.%d]\t%s\t%s\n", date_str, process_flag, SwooleG.pid, process_id, level_str, cnt);

if (write(SwooleG.log_fd, log_str, n) < 0)
{
//write to log failed.
}
write(SwooleG.log_fd, log_str, n);
}
8 changes: 4 additions & 4 deletions src/factory/FactoryProcess.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,18 @@ static int swFactoryProcess_finish(swFactory *factory, swSendData *resp)
swConnection *conn = swServer_connection_verify(serv, fd);
if (!conn)
{
swWarn("session#%d does not exist.", fd);
swoole_error_log(SW_LOG_NOTICE, SW_ERROR_SESSION_NO_EXIST, "session#%d does not exist.", fd);
return SW_ERR;
}
else if ((conn->closed || conn->removed) && resp->info.type != SW_EVENT_CLOSE)
{
int _len = resp->length > 0 ? resp->length : resp->info.len;
swWarn("send %d byte failed, because session#%d is closed.", _len, fd);
swoole_error_log(SW_LOG_NOTICE, SW_ERROR_SESSION_CLOSED, "send %d byte failed, because session#%d is closed.", _len, fd);
return SW_ERR;
}
else if (conn->overflow)
{
swWarn("send failed, session#%d output buffer has been overflowed.", fd);
swoole_error_log(SW_LOG_WARNING, SW_ERROR_OUTPUT_BUFFER_OVERFLOW, "send failed, session#%d output buffer has been overflowed.", fd);
return SW_ERR;
}

Expand Down Expand Up @@ -275,7 +275,7 @@ static int swFactoryProcess_end(swFactory *factory, int fd)
}
else if (conn->closing)
{
swRuntimeError(SW_ERROR_SERVER_WORKER_CLOSING, "The connection[%d] is closing.", fd);
swoole_error_log(SW_LOG_NOTICE, SW_ERROR_SESSION_CLOSING, "The connection[%d] is closing.", fd);
return SW_ERR;
}
else if (conn->closed)
Expand Down
2 changes: 1 addition & 1 deletion src/network/ReactorThread.c
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ static int swReactorThread_onPipeWrite(swReactor *reactor, swEvent *ev)
#endif
if (conn && conn->closed)
{
swRuntimeError(SW_ERROR_SESSION_CLOSED_BY_SERVER, "Session#%d is closed by server.", send_data->info.fd);
swoole_error_log(SW_LOG_NOTICE, SW_ERROR_SESSION_CLOSED_BY_SERVER, "Session#%d is closed by server.", send_data->info.fd);
}
swBuffer_pop_trunk(buffer, trunk);
continue;
Expand Down
22 changes: 11 additions & 11 deletions src/network/Server.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ int swServer_master_onAccept(swReactor *reactor, swEvent *event)
//too many connection
if (new_fd >= serv->max_connection)
{
swWarn("Too many connections [now: %d].", new_fd);
swoole_error_log(SW_LOG_WARNING, SW_ERROR_SERVER_TOO_MANY_SOCKET, "Too many connections [now: %d].", new_fd);
close(new_fd);
return SW_OK;
}
Expand Down Expand Up @@ -457,9 +457,9 @@ int swServer_start(swServer *serv)
serv->message_queue_key = ftok(path_ptr, 1);
}
//init loggger
if (serv->log_file[0] != 0)
if (SwooleG.log_file)
{
swLog_init(serv->log_file);
swLog_init(SwooleG.log_file);
}
//run as daemon
if (serv->daemonize > 0)
Expand All @@ -471,7 +471,7 @@ int swServer_start(swServer *serv)
{
if (dup2(SwooleG.log_fd, STDOUT_FILENO) < 0)
{
swWarn("dup2() failed. Error: %s[%d]", strerror(errno), errno);
swoole_error_log(SW_LOG_ERROR, SW_ERROR_SYSTEM_CALL_FAIL, "dup2() failed. Error: %s[%d]", strerror(errno), errno);
}
}
/**
Expand All @@ -484,16 +484,16 @@ int swServer_start(swServer *serv)
{
if (dup2(SwooleG.null_fd, STDOUT_FILENO) < 0)
{
swWarn("dup2(STDOUT_FILENO) failed. Error: %s[%d]", strerror(errno), errno);
swoole_error_log(SW_LOG_ERROR, SW_ERROR_SYSTEM_CALL_FAIL, "dup2(STDOUT_FILENO) failed. Error: %s[%d]", strerror(errno), errno);
}
if (dup2(SwooleG.null_fd, STDERR_FILENO) < 0)
{
swWarn("dup2(STDERR_FILENO) failed. Error: %s[%d]", strerror(errno), errno);
swoole_error_log(SW_LOG_ERROR, SW_ERROR_SYSTEM_CALL_FAIL, "dup2(STDERR_FILENO) failed. Error: %s[%d]", strerror(errno), errno);
}
}
else
{
swWarn("open(/dev/null) failed. Error: %s[%d]", strerror(errno), errno);
swoole_error_log(SW_LOG_ERROR, SW_ERROR_SYSTEM_CALL_FAIL, "open(/dev/null) failed. Error: %s[%d]", strerror(errno), errno);
}
}

Expand All @@ -520,7 +520,7 @@ int swServer_start(swServer *serv)
serv->workers = SwooleG.memory_pool->alloc(SwooleG.memory_pool, serv->worker_num * sizeof(swWorker));
if (serv->workers == NULL)
{
swWarn("[Master] malloc[object->workers] failed");
swoole_error_log(SW_LOG_ERROR, SW_ERROR_SYSTEM_CALL_FAIL, "gmalloc[object->workers] failed");
return SW_ERR;
}

Expand Down Expand Up @@ -630,7 +630,7 @@ int swServer_create(swServer *serv)
{
if (SwooleG.main_reactor)
{
swRuntimeError(SW_ERROR_SERVER_MUST_CREATED_BEFORE_CLIENT, "The swoole_server must create before client");
swoole_error_log(SW_LOG_ERROR, SW_ERROR_SERVER_MUST_CREATED_BEFORE_CLIENT, "The swoole_server must create before client");
return SW_ERR;
}

Expand Down Expand Up @@ -728,7 +728,7 @@ int swServer_free(swServer *serv)
sw_shm_free(serv->session_list);
}
//close log file
if (serv->log_file[0] != 0)
if (SwooleG.log_file != 0)
{
swLog_free();
}
Expand Down Expand Up @@ -799,7 +799,7 @@ int swServer_tcp_send(swServer *serv, int fd, void *data, uint32_t length)
*/
if (length >= serv->buffer_output_size)
{
swWarn("More than the output buffer size[%d], please use the sendfile.", serv->buffer_output_size);
swoole_error_log(SW_LOG_WARNING, SW_ERROR_OUTPUT_BUFFER_OVERFLOW, "More than the output buffer size[%d], please use the sendfile.", serv->buffer_output_size);
return SW_ERR;
}
else
Expand Down
2 changes: 2 additions & 0 deletions swoole_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@

//#define SW_CONNECTION_LIST_EXPAND (4096*2) //动态扩容的数量

#define SW_HOST_MAXSIZE 128

//#define SW_DEBUG //debug
#define SW_LOG_NO_SRCINFO //no source info
#define SW_LOG_TRACE_OPEN 0
Expand Down
13 changes: 7 additions & 6 deletions swoole_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -1414,12 +1414,13 @@ PHP_METHOD(swoole_server, set)
if (sw_zend_hash_find(vht, ZEND_STRS("log_file"), (void **) &v) == SUCCESS)
{
convert_to_string(v);
if (Z_STRLEN_P(v) > SW_LOG_FILENAME)
{
swoole_php_fatal_error(E_ERROR, "log_file name to long");
RETURN_FALSE;
}
memcpy(serv->log_file, Z_STRVAL_P(v), Z_STRLEN_P(v));
SwooleG.log_file = strndup(Z_STRVAL_P(v), Z_STRLEN_P(v));
}
//log_level
if (sw_zend_hash_find(vht, ZEND_STRS("log_level"), (void **) &v) == SUCCESS)
{
convert_to_long(v);
SwooleG.log_level = (int) Z_LVAL_P(v);
}
/**
* for dispatch_mode = 1/3
Expand Down

0 comments on commit 496d547

Please sign in to comment.