Skip to content

Commit fc473b9

Browse files
committed
refactor file module
1 parent 25f1d7c commit fc473b9

File tree

21 files changed

+454
-88
lines changed

21 files changed

+454
-88
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,4 @@ src/platform/Android/app/src/main/cpp/.idea
5252
src/platform/Android/sdk/.cxx
5353
demo/Android/.idea
5454
src/http/.idea
55+
config.json

CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@ option(DISABLE_TEST "disable test" OFF)
2727
option(DISABLE_HTTP "disable http" OFF)
2828

2929
macro(read_config)
30-
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/config.json CONFIG_JSON_STRING)
30+
# Check if config.json exists
31+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/config.json)
32+
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/config.json CONFIG_JSON_STRING)
33+
else()
34+
# Provide default configuration if file doesn't exist
35+
set(CONFIG_JSON_STRING "{\"platform\": \"PC\", \"build_type\": \"Debug\", \"disable_http\": false, \"disable_test\": false }")
36+
message(WARNING "config.json not found, using default configuration: ${CONFIG_JSON_STRING}")
37+
endif()
3138

3239
#parse build platform
3340
string(JSON BUILD_PLATFORM_VALUE GET ${CONFIG_JSON_STRING} platform)
@@ -81,7 +88,6 @@ macro(add_core_file TARGET_NAME)
8188
src/core/audio
8289
src/core/codec
8390
src/core/video
84-
src/core/opengles
8591
)
8692
endmacro()
8793

config.json

Lines changed: 0 additions & 6 deletions
This file was deleted.
Binary file not shown.

src/base/File.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "File.h"
88
#include "Log.hpp"
99

10-
namespace slark::FileUtil {
10+
namespace slark::File {
1111

1212
IFile::IFile(std::string path, FileMode mode)
1313
: path_(std::move(path))
@@ -40,7 +40,7 @@ bool IFile::open() noexcept {
4040
void IFile::seek(int64_t offset) noexcept {
4141
if (file_) {
4242
fseeko(file_, offset, SEEK_SET);
43-
LogI("seek to offset:{}, file size:{}", offset, FileUtil::fileSize(path_));
43+
LogI("seek to offset:{}, file size:{}", offset, File::fileSize(path_));
4444
}
4545
}
4646

@@ -53,7 +53,7 @@ uint64_t IFile::tell() const noexcept {
5353
}
5454

5555
uint64_t IFile::fileSize() const noexcept {
56-
return FileUtil::fileSize(path_);
56+
return File::fileSize(path_);
5757
}
5858

5959
void IFile::close() noexcept {
@@ -221,4 +221,4 @@ void ReadFile::close() noexcept {
221221
}
222222
}
223223

224-
}//end namespace slark::FileUtil
224+
}//end namespace slark::File

src/base/File.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "FileUtil.h"
1414
#include "Data.hpp"
1515

16-
namespace slark::FileUtil {
16+
namespace slark::File {
1717

1818
using FileHandle = FILE*;
1919

src/base/FileUtil.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "FileUtil.h"
77
#include <filesystem>
88

9-
namespace slark::FileUtil {
9+
namespace slark::File {
1010

1111
#if !SLARK_IOS && !SLARK_ANDROID
1212
std::string rootPath() noexcept {

src/base/FileUtil.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <cstdint>
1010
#include <string>
1111

12-
namespace slark::FileUtil {
12+
namespace slark::File {
1313

1414
[[maybe_unused]] extern std::string rootPath() noexcept;
1515

src/base/Log.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ void printLog(const std::string& log);
3131
template <typename ...Args>
3232
void outputLog(LogType level, std::string_view format, Args&& ...args) {
3333
auto logStr = std::vformat(format, std::make_format_args(args...));
34-
LogOutput::shareInstance().write(logStr);
34+
if (level != LogType::Print) {
35+
LogOutput::shareInstance().write(logStr);
36+
}
3537
if (level == LogType::Record) {
3638
return;
3739
}

src/base/LogOutput.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace slark {
1212

1313
std::string LogFileName() {
14-
using namespace slark::FileUtil;
14+
using namespace slark::File;
1515
const std::string logDir = cachePath() + "/logs";
1616
if (!isDirExist(logDir) && !createDir(logDir)) {
1717
LogP("create log folder failed.");
@@ -31,24 +31,25 @@ LogOutput& LogOutput::shareInstance(){
3131
return *instance_;
3232
}
3333

34-
LogOutput::LogOutput() = default;
34+
LogOutput::LogOutput() {
35+
writer_.withLock([&](auto& writer){
36+
writer = std::make_unique<Writer>();
37+
recreateFile(writer);
38+
});
39+
}
3540

3641
LogOutput::~LogOutput() = default;
3742

3843
void LogOutput::write(const std::string& str) noexcept {
3944
writer_.withLock([&](auto& writer){
40-
if (writer == nullptr) {
41-
writer = std::make_unique<Writer>();
42-
updateFile(writer);
43-
}
4445
writer->write(str);
4546
if (writer->writeCount() >= kMaxWriteLogCount) {
46-
updateFile(writer);
47+
recreateFile(writer);
4748
}
4849
});
4950
}
5051

51-
void LogOutput::updateFile(std::unique_ptr<Writer>& writer) noexcept {
52+
void LogOutput::recreateFile(std::unique_ptr<Writer>& writer) noexcept {
5253
auto path = LogFileName();
5354
if (path.empty()) {
5455
LogP("log file path is empty");

0 commit comments

Comments
 (0)