Example #1
0
int log_set_prefix(const char *base_path, const char *filename_prefix)
{
	int result;
	char logfile[LOG_MAX_PATH_SIZE];

	if (!logger_init_flag) {
		fprintf(stderr, "log module may be not init!\n");
		return 1;	
	}
	
	if (!base_path || !filename_prefix) {
		fprintf(stderr, "base_path or filename_prefix can not be empty\n");
		return 1;	
	}

	if ((strlen(base_path) + strlen(filename_prefix) + sizeof("/log/.log") - 1) >= LOG_MAX_PATH_SIZE) {
		fprintf(stderr, "full file name of log file can not be longer than %d\n", LOG_MAX_PATH_SIZE);
		return 1;
	}

	if ((result = check_and_mk_log_dir(base_path)) != 0) {
		return result;
	}

	snprintf(logfile, LOG_MAX_PATH_SIZE, "%s/"LOG_PATH"/%s.log", base_path, filename_prefix);

	if ((pContext->log_fd = open(logfile, O_WRONLY | O_CREAT | O_APPEND, 0644)) < 0) {
		fprintf(stderr, "open log file \"%s\" to write fail, errno: %d, error info: %s", logfile, errno, STRERROR(errno));
		pContext->log_fd = STDERR_FILENO;
		return (errno != 0) ? errno : EACCES;
	}

	if (!pContext->log_base_path) {
		pContext->log_base_path = (char *)malloc(LOG_MAX_PATH_SIZE);

		if (pContext->log_base_path == NULL) {
			fprintf(stderr, "malloc %d bytes fail, errno: %d, error info: %s", LOG_MAX_PATH_SIZE, errno, STRERROR(errno));
			return (errno != 0) ? errno : ENOMEM;
		}

		pContext->log_filename_prefix = (char *)malloc(LOG_MAX_PATH_SIZE);

		if (pContext->log_filename_prefix == NULL) {
			free(pContext->log_base_path);
			pContext->log_base_path = NULL;

			fprintf(stderr, "malloc %d bytes fail, errno: %d, error info: %s", LOG_MAX_PATH_SIZE, errno, STRERROR(errno));
			return (errno != 0) ? errno : ENOMEM;
		}
	}

	strcpy(pContext->log_base_path, base_path);
	strcpy(pContext->log_filename_prefix, filename_prefix);

	return 0;
}
Example #2
0
int log_set_prefix_ex(LogContext *pContext, const char *base_path, \
		const char *filename_prefix)
{
	int result;

	if ((result=check_and_mk_log_dir(base_path)) != 0)
	{
		return result;
	}

	snprintf(pContext->log_filename, MAX_PATH_SIZE, "%s/logs/%s.log", \
		base_path, filename_prefix);

	return log_open(pContext);
}