From 0d835b18487132c3b8e40d0c9811b099fded79f7 Mon Sep 17 00:00:00 2001 From: "yuchen.cc" Date: Fri, 25 Aug 2023 19:23:09 +0800 Subject: [PATCH] refactor some logs Signed-off-by: yuchen.cc --- src/bk_download.cpp | 3 +-- src/image_file.cpp | 12 +++++------- src/overlaybd/extfs/extfs.cpp | 6 ++---- src/overlaybd/tar/libtar.cpp | 32 ++++++++++---------------------- src/overlaybd/tar/tar_file.cpp | 3 +++ src/overlaybd/tar/whiteout.cpp | 6 ++---- src/switch_file.cpp | 7 +++---- 7 files changed, 26 insertions(+), 43 deletions(-) diff --git a/src/bk_download.cpp b/src/bk_download.cpp index 74dd0339..f0690ed1 100644 --- a/src/bk_download.cpp +++ b/src/bk_download.cpp @@ -128,8 +128,7 @@ bool BkDownload::download_done() { int ret = lfs->rename(old_name.c_str(), new_name.c_str()); if (ret != 0) { - LOG_ERROR("rename(`,`), `:`", old_name, new_name, errno, strerror(errno)); - return false; + LOG_ERRNO_RETURN(0, false, "rename(`,`) failed", old_name, new_name); } LOG_INFO("download verify done. rename(`,`) success", old_name, new_name); return true; diff --git a/src/image_file.cpp b/src/image_file.cpp index 7a1b5f25..c31df0a4 100644 --- a/src/image_file.cpp +++ b/src/image_file.cpp @@ -58,7 +58,7 @@ IFile *ImageFile::__open_ro_file(const std::string &path) { auto file = open_localfile_adaptor(path.c_str(), flags, 0644, ioengine); if (!file) { set_failed("failed to open local file " + path); - LOG_ERROR_RETURN(0, nullptr, "open(`),`:`", path, errno, strerror(errno)); + LOG_ERRNO_RETURN(0, nullptr, "open(`) failed", path); } if (flags & O_DIRECT) { @@ -67,8 +67,7 @@ IFile *ImageFile::__open_ro_file(const std::string &path) { if (!aligned_file) { set_failed("failed to open aligned_file_adaptor " + path); delete file; - LOG_ERROR_RETURN(0, nullptr, "new_aligned_file_adaptor(`) failed, `:`", path, errno, - strerror(errno)); + LOG_ERRNO_RETURN(0, nullptr, "new_aligned_file_adaptor(`) failed", path); } file = aligned_file; } @@ -77,8 +76,7 @@ IFile *ImageFile::__open_ro_file(const std::string &path) { if (!switch_file) { set_failed("failed to open switch file `" + path); delete file; - LOG_ERROR_RETURN(0, nullptr, "new_switch_file(`) failed, `,:`", path, errno, - strerror(errno)); + LOG_ERRNO_RETURN(0, nullptr, "new_switch_file(`) failed", path); } file = switch_file; @@ -89,7 +87,7 @@ IFile *ImageFile::__open_ro_target_file(const std::string &path) { auto file = open_localfile_adaptor(path.c_str(), O_RDONLY, 0644, 0); if (!file) { set_failed("failed to open local data file " + path); - LOG_ERROR_RETURN(0, nullptr, "open(`),`:`", path, errno, strerror(errno)); + LOG_ERRNO_RETURN(0, nullptr, "open(`) failed", path); } return file; } @@ -281,7 +279,7 @@ int ImageFile::open_lower_layer(IFile *&file, ImageConfigNS::LayerConfig &layer, auto gz_index = open_localfile_adaptor(layer.gzipIndex().c_str(), O_RDONLY, 0644, 0); if (!gz_index) { set_failed("failed to open gzip index " + layer.gzipIndex()); - LOG_ERROR_RETURN(0, -1, "open(`),`:`", layer.gzipIndex(), errno, strerror(errno)); + LOG_ERRNO_RETURN(0, -1, "open(`) failed", layer.gzipIndex()); } target_file = new_gzfile(target_file, gz_index, true); if (image_service.global_conf.gzipCacheConfig().enable() && layer.targetDigest() != "") { diff --git a/src/overlaybd/extfs/extfs.cpp b/src/overlaybd/extfs/extfs.cpp index ce7ac8ce..a197fcb9 100644 --- a/src/overlaybd/extfs/extfs.cpp +++ b/src/overlaybd/extfs/extfs.cpp @@ -57,15 +57,13 @@ ext2_filsys do_ext2fs_open(io_manager extfs_manager) { ); if (ret) { errno = -parse_extfs_error(nullptr, 0, ret); - LOG_ERROR("failed ext2fs_open, errno `:`", errno, strerror(errno)); - return nullptr; + LOG_ERRNO_RETURN(0, nullptr, "failed ext2fs_open"); } ret = ext2fs_read_bitmaps(fs); if (ret) { errno = -parse_extfs_error(fs, 0, ret); - LOG_ERROR("failed ext2fs_read_bitmaps, errno `:`", errno, strerror(errno)); ext2fs_close(fs); - return nullptr; + LOG_ERRNO_RETURN(0, nullptr, "failed ext2fs_read_bitmaps"); } LOG_INFO("ext2fs opened"); return fs; diff --git a/src/overlaybd/tar/libtar.cpp b/src/overlaybd/tar/libtar.cpp index 7781ea7c..866391bb 100644 --- a/src/overlaybd/tar/libtar.cpp +++ b/src/overlaybd/tar/libtar.cpp @@ -44,15 +44,13 @@ int UnTar::set_file_perms(const char *filename) { /* change owner/group */ if (geteuid() == 0) { if (fs->lchown(filename, uid, gid) == -1) { - LOG_ERROR("lchown failed, filename `, `", filename, strerror(errno)); - return -1; + LOG_ERRNO_RETURN(0, -1, "lchown failed, filename `, uid `, gid `", filename, uid, gid); } } /* change access/modification time */ if (fs->lutimes(filename, tv) == -1) { - LOG_ERROR("lutimes failed, filename `, `", filename, strerror(errno)); - return -1; + LOG_ERRNO_RETURN(0, -1, "lutimes failed, filename `", filename); } /* change permissions */ @@ -69,8 +67,7 @@ int UnTar::set_file_perms(const char *filename) { return 0; } if (fs->chmod(filename, mode) == -1) { - LOG_ERROR("chmod failed `", strerror(errno)); - return -1; + LOG_ERRNO_RETURN(0, -1, "chmod failed, filename `, mode `", filename, mode); } return 0; @@ -95,8 +92,7 @@ int UnTar::extract_all() { while ((i = read_header()) == 0) { if (extract_file() != 0) { - LOG_ERROR("extract failed, filename `, `", get_pathname(), strerror(errno)); - return -1; + LOG_ERRNO_RETURN(0, -1, "extract failed, filename `", get_pathname()); } if (TH_ISDIR(header)) { dirs.emplace_back(std::make_pair(std::string(get_pathname()), header.get_mtime())); @@ -111,8 +107,7 @@ int UnTar::extract_all() { tv[0].tv_sec = tv[1].tv_sec = dir.second; tv[0].tv_usec = tv[1].tv_usec = 0; if (fs->lutimes(path.c_str(), tv) == -1) { - LOG_ERROR("utime failed, filename `, `", dir.first.c_str(), strerror(errno)); - return -1; + LOG_ERRNO_RETURN(0, -1, "utime failed, filename `", dir.first.c_str()); } } @@ -151,15 +146,11 @@ int UnTar::extract_file() { } else { if (!S_ISDIR(s.st_mode)) { if (fs->unlink(npath.c_str()) == -1 && errno != ENOENT) { - LOG_ERROR("remove exist file ` failed, `", npath.c_str(), strerror(errno)); - errno = EEXIST; - return -1; + LOG_ERRNO_RETURN(EEXIST, -1, "remove exist file ` failed", npath.c_str()); } } else if (!TH_ISDIR(header)) { if (remove_all(npath) == -1) { - LOG_ERROR("remove exist dir ` failed, `", npath.c_str(), strerror(errno)); - errno = EEXIST; - return -1; + LOG_ERRNO_RETURN(EEXIST, -1, "remove exist dir ` failed", npath.c_str()); } } } @@ -290,8 +281,7 @@ int UnTar::extract_hardlink(const char *filename) { char *linktgt = get_linkname(); LOG_DEBUG(" ==> extracting: ` (link to `)", filename, linktgt); if (fs->link(linktgt, filename) == -1) { - LOG_ERROR("link failed, `", strerror(errno)); - return -1; + LOG_ERRNO_RETURN(0, -1, "link failed, filename `, linktgt `", filename, linktgt); } return 0; } @@ -300,8 +290,7 @@ int UnTar::extract_symlink(const char *filename) { char *linktgt = get_linkname(); LOG_DEBUG(" ==> extracting: ` (symlink to `)", filename, linktgt); if (fs->symlink(linktgt, filename) == -1) { - LOG_ERROR("symlink failed, `", strerror(errno)); - return -1; + LOG_ERRNO_RETURN(0, -1, "symlink failed, filename `, linktgt `", filename, linktgt); } return 0; } @@ -327,8 +316,7 @@ int UnTar::extract_block_char_fifo(const char *filename) { LOG_DEBUG(" ==> extracting: ` (block/char/fifo `,`)", filename, devmaj, devmin); if (fs->mknod(filename, mode, makedev(devmaj, devmin)) == -1) { - LOG_ERROR("block/char/fifo failed, `", strerror(errno)); - return -1; + LOG_ERRNO_RETURN(0, -1, "block/char/fifo failed, filename `", filename); } return 0; diff --git a/src/overlaybd/tar/tar_file.cpp b/src/overlaybd/tar/tar_file.cpp index ee520d2d..4923705c 100644 --- a/src/overlaybd/tar/tar_file.cpp +++ b/src/overlaybd/tar/tar_file.cpp @@ -332,6 +332,9 @@ IFile *new_tar_file(IFile *file, bool create) { } IFile *open_tar_file(IFile *file) { + if (!file) { + LOG_ERROR_RETURN(0, nullptr, "file is nullptr"); + } auto ret = is_tar_file(file); if (ret == 1) { LOG_INFO("open file as tar file"); diff --git a/src/overlaybd/tar/whiteout.cpp b/src/overlaybd/tar/whiteout.cpp index ceee6b1d..fa5c0d4f 100644 --- a/src/overlaybd/tar/whiteout.cpp +++ b/src/overlaybd/tar/whiteout.cpp @@ -59,14 +59,12 @@ int UnTar::remove_all(const std::string &path, bool rmdir) { return 0; } } else { - LOG_ERROR("get path ` stat failed, errno `:`", path, errno, strerror(errno)); - return -1; + LOG_ERRNO_RETURN(0, -1, "get path ` stat failed", path); } auto dirs = fs->opendir(path.c_str()); if (dirs == nullptr) { - LOG_ERROR("open dir ` failed, errno `:`", path, errno, strerror(errno)); - return -1; + LOG_ERRNO_RETURN(0, -1, "open dir ` failed", path); } dirent *dirInfo; while ((dirInfo = dirs->get()) != nullptr) { diff --git a/src/switch_file.cpp b/src/switch_file.cpp index 57815c9d..66bd1442 100644 --- a/src/switch_file.cpp +++ b/src/switch_file.cpp @@ -46,13 +46,12 @@ static IFile *try_open_zfile(IFile *file, bool verify, const char *file_path) { if (is_zfile == 1) { auto zf = ZFile::zfile_open_ro(file, verify, true); if (!zf) { - LOG_ERROR_RETURN(0, nullptr, "zfile_open_ro failed, path: `: error: `(`)", file_path, - errno, strerror(errno)); + LOG_ERRNO_RETURN(0, nullptr, "zfile_open_ro failed, path: `", file_path); } - LOG_INFO("open file as zfile"); + LOG_INFO("open file as zfile format, path: `", file_path); return zf; } - LOG_INFO("file is not zfile format"); + LOG_INFO("file is not zfile format, path: `", file_path); return file; }