diff --git a/cli/src/basic.h b/cli/src/basic.h index b930316..44620b4 100644 --- a/cli/src/basic.h +++ b/cli/src/basic.h @@ -22,6 +22,7 @@ #define DEFAULT_DIR_MODE 0755 #define BUF_SIZE 1024 #define MAX_DEVICE_NR 1024 +#define DEFAULT_LOG_FILE "/var/log/ascend-docker-runtime.log" #define ALLOW_PATH "/devices.allow" #define ROOT_GAP 4 diff --git a/cli/src/logging.c b/cli/src/logging.c index a4c5411..e8dad21 100644 --- a/cli/src/logging.c +++ b/cli/src/logging.c @@ -5,22 +5,109 @@ #include "logging.h" #include +#include #include +#include +#include +#include +#include +#include "securec.h" +#include "basic.h" + +static int g_pid = -1; +static FILE *g_logFile = NULL; + +void SetPidForLog(int pid) +{ + g_pid = pid; +} + +int OpenLog(const char *logFile) +{ + char realPath[PATH_MAX] = {0}; + + if (g_logFile != NULL) { + return 0; // 防重入 + } + + if (realpath(logFile, realPath) == NULL && errno != ENOENT) { + LogError("error: cannot canonicalize log file path %s.", logFile); + return -1; + } + + g_logFile = fopen((const char *)realPath, "ae"); + if (g_logFile == NULL) { + LogError("error: failed to open log file %s.", realPath); + return -1; + } + + return 0; +} + +void CloseLog() +{ + if (g_logFile != NULL) { + (void)fclose(g_logFile); + g_logFile = NULL; + } +} + +static void WriteLog(char level, const char *fmt, va_list args) +{ + struct timeval tv = {0}; + struct tm *tm = NULL; + char buf[BUF_SIZE] = {0}; + + if (g_logFile == NULL) { + return; + } + + if (gettimeofday(&tv, NULL) < 0 || + (tm = gmtime(&tv.tv_sec)) == NULL || + strftime(buf, sizeof(buf), "%m%d %T", tm) == 0) { + strcpy_s(buf, sizeof(buf), "0000 00:00:00"); + } + + fprintf(g_logFile, "[%c %s.%06ld %d] ", level, buf, tv.tv_usec, g_pid); + vfprintf(g_logFile, fmt, args); + fputc('\n', g_logFile); +} void LogError(const char *fmt, ...) { va_list args; + va_list logArgs; va_start(args, fmt); + va_copy(logArgs, args); vfprintf(stderr, fmt, args); + WriteLog('E', fmt, logArgs); + va_end(logArgs); va_end(args); } void LogInfo(const char *fmt, ...) { va_list args; + va_list logArgs; va_start(args, fmt); + va_copy(logArgs, args); vfprintf(stdout, fmt, args); + WriteLog('I', fmt, logArgs); + va_end(logArgs); + va_end(args); +} + +void LogWarning(const char *fmt, ...) +{ + va_list args; + va_list logArgs; + + va_start(args, fmt); + va_copy(logArgs, args); + vfprintf(stderr, fmt, args); + WriteLog('W', fmt, logArgs); + va_end(logArgs); va_end(args); } \ No newline at end of file diff --git a/cli/src/logging.h b/cli/src/logging.h index c0d31e3..eb25a4e 100644 --- a/cli/src/logging.h +++ b/cli/src/logging.h @@ -5,7 +5,12 @@ #ifndef _LOGGING_H #define _LOGGING_H +void SetPidForLog(int pid); +int OpenLog(const char *logFile); +void CloseLog(); + void LogError(const char *fmt, ...); void LogInfo(const char *fmt, ...); +void LogWarning(const char *fmt, ...); #endif diff --git a/cli/src/main.c b/cli/src/main.c index 6cdf36b..23262e6 100644 --- a/cli/src/main.c +++ b/cli/src/main.c @@ -201,12 +201,6 @@ int DoPrepare(const struct CmdArgs *args, struct ParsedConfig *config) return -1; } - ret = ParseRuntimeOptions(args->options); - if (ret < 0) { - LogError("error: failed to parse runtime options."); - return -1; - } - return 0; } @@ -277,11 +271,34 @@ int Process(int argc, char **argv) return -1; } + ret = ParseRuntimeOptions(args.options); + if (ret < 0) { + LogError("error: failed to parse runtime options."); + return -1; + } + + SetPidForLog(args.pid); + + if (IsOptionVerboseSet()) { + ret = OpenLog(DEFAULT_LOG_FILE); + if (ret < 0) { + LogError("error: failed to open log file %s.", DEFAULT_LOG_FILE); + return -1; + } + } + ret = SetupContainer(&args); if (ret < 0) { + if (IsOptionVerboseSet()) { + CloseLog(); + } return ret; } + if (IsOptionVerboseSet()) { + CloseLog(); + } + return 0; } diff --git a/cli/src/mount.c b/cli/src/mount.c index 30dd7ca..6ef04c4 100644 --- a/cli/src/mount.c +++ b/cli/src/mount.c @@ -147,8 +147,8 @@ int MountFile(const char *rootfs, const char *filepath) struct stat srcStat; ret = stat(filepath, &srcStat); if (ret < 0) { - LogError("error: failed to stat src: %s.", filepath); - return -1; + LogWarning("warning: failed to find file %s on host, skipping", filepath); + return 0; } ret = CreateFile(dst, srcStat.st_mode); @@ -179,7 +179,8 @@ int MountDir(const char *rootfs, const char *src) struct stat srcStat; ret = stat(src, &srcStat); if (ret < 0) { - return -1; + LogWarning("warning: failed to find dir %s on host, skipping", src); + return 0; } ret = MakeDirWithParent(dst, DEFAULT_DIR_MODE); diff --git a/cli/src/options.c b/cli/src/options.c index 7c644fd..db65922 100644 --- a/cli/src/options.c +++ b/cli/src/options.c @@ -10,6 +10,7 @@ static struct { bool noDrv; + bool verbose; } g_runtimeOptions; static struct { @@ -17,6 +18,7 @@ static struct { bool *flag; } g_optionNameFlagTable[] = { {"NODRV", &g_runtimeOptions.noDrv}, // 不挂载Driver + {"VERBOSE", &g_runtimeOptions.verbose}, // 输出日志 {NULL, NULL} }; @@ -24,6 +26,7 @@ int ParseRuntimeOptions(const char *options) { // set defaults value g_runtimeOptions.noDrv = false; + g_runtimeOptions.verbose = false; static const char *seperator = ","; char *runtimeOptions = strdup(options); @@ -50,4 +53,9 @@ int ParseRuntimeOptions(const char *options) bool IsOptionNoDrvSet() { return g_runtimeOptions.noDrv; +} + +bool IsOptionVerboseSet() +{ + return g_runtimeOptions.verbose; } \ No newline at end of file diff --git a/cli/src/options.h b/cli/src/options.h index 713c2fa..178e67c 100644 --- a/cli/src/options.h +++ b/cli/src/options.h @@ -9,5 +9,6 @@ int ParseRuntimeOptions(const char *options); bool IsOptionNoDrvSet(); +bool IsOptionVerboseSet(); #endif diff --git a/cli/src/utils.c b/cli/src/utils.c index 85bfb36..56cfc3a 100644 --- a/cli/src/utils.c +++ b/cli/src/utils.c @@ -42,11 +42,9 @@ int CheckDirExists(const char *dir) { DIR *ptr = opendir(dir); if (NULL == ptr) { - LogError("path %s not exist.", dir); return -1; } - LogInfo("path %s exist.", dir); closedir(ptr); return 0; } diff --git a/hook/main.go b/hook/main.go index 4c12347..3b9c96e 100644 --- a/hook/main.go +++ b/hook/main.go @@ -40,6 +40,7 @@ var ( var validRuntimeOptions = [...]string { "NODRV", + "VERBOSE", } type containerConfig struct {