Skip to content
This repository was archived by the owner on Feb 7, 2026. It is now read-only.

Commit cc86bd8

Browse files
author
tomasmark79
committed
updated: huge update for Logger support - Implemented dependency injection
1 parent 31c68d3 commit cc86bd8

File tree

13 files changed

+945
-894
lines changed

13 files changed

+945
-894
lines changed

include/DotNameLib/DotNameLib.hpp

Lines changed: 79 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,94 @@
11
#pragma once
22

33
#include <DotNameLib/version.h> // first configuration will create this file
4+
#include <Logger/ILogger.hpp>
5+
#include <Assets/AssetContext.hpp>
6+
#include <Utils/Utils.hpp>
47
#include <filesystem>
8+
#include <memory>
59
#include <string>
610

7-
// Public API
11+
namespace dotnamecpp::v1 {
12+
class DotNameLib {
813

9-
namespace dotname {
10-
// Version information
11-
inline namespace v1 {
14+
private:
15+
const std::string libName_ = "DotNameLib v." DOTNAMELIB_VERSION;
16+
std::shared_ptr<dotnamecpp::logging::ILogger> logger_;
17+
std::filesystem::path assetsPath_;
18+
bool isInitialized_ = false;
1219

13-
class DotNameLib {
14-
private:
15-
const std::string libName_ = "DotNameLib v." DOTNAMELIB_VERSION;
16-
std::filesystem::path assetsPath_;
17-
bool isInitialized_ = false;
20+
public:
21+
explicit DotNameLib (std::shared_ptr<dotnamecpp::logging::ILogger> logger,
22+
const std::filesystem::path& assetsPath)
23+
: logger_ (std::move (logger)), assetsPath_ (assetsPath) {
1824

19-
public:
20-
DotNameLib ();
21-
explicit DotNameLib (const std::filesystem::path& assetsPath);
22-
~DotNameLib ();
25+
if (assetsPath.empty ()) {
26+
logger_->warning ("Empty assets path provided");
27+
return;
28+
}
29+
try {
30+
assetsPath_ = assetsPath;
31+
AssetContext::setAssetsPath (assetsPath);
32+
logger_->infoStream () << libName_ << " initialized with assets path: "
33+
<< AssetContext::getAssetsPath ();
34+
logger_->debugStream () << "Assets: " << AssetContext::getAssetsPath ();
35+
logger_->debugStream () << DotNameUtils::json::getCustomStringSign ();
36+
37+
// Check if logo file exists before trying to open it
38+
const auto logoPath = AssetContext::getAssetsPath () / "DotNameCppLogo.svg";
39+
logger_->debugStream () << "Logo path: " << logoPath;
2340

24-
// Rule of 5 for better resource management
25-
DotNameLib (const DotNameLib& other) = delete;
26-
DotNameLib& operator= (const DotNameLib& other) = delete;
27-
DotNameLib (DotNameLib&& other) noexcept;
28-
DotNameLib& operator= (DotNameLib&& other) noexcept;
41+
if (std::filesystem::exists (logoPath)) {
42+
std::ifstream logoFile (logoPath);
43+
if (logoFile.is_open ()) {
44+
logger_->debugStream () << "Logo file successfully opened";
45+
isInitialized_ = true;
46+
} else {
47+
logger_->warningStream () << "Could not open logo file: " << logoPath;
48+
}
49+
} else {
50+
logger_->warningStream () << "Logo file does not exist: " << logoPath;
51+
}
52+
} catch (const std::exception& e) {
53+
logger_->errorStream () << "Error initializing DotNameLib: " << e.what ();
54+
isInitialized_ = false;
55+
}
56+
}
2957

30-
// Public interface
31-
[[nodiscard]] bool isInitialized () const noexcept {
32-
return isInitialized_;
58+
// Rule of 5 for better resource management
59+
DotNameLib (const DotNameLib& other) = delete;
60+
DotNameLib& operator= (const DotNameLib& other) = delete;
61+
DotNameLib (DotNameLib&& other) noexcept : assetsPath_ (std::move (other.assetsPath_)),
62+
isInitialized_ (other.isInitialized_) {
63+
other.isInitialized_ = false;
64+
logger_->infoStream () << libName_ << " move constructed";
65+
}
66+
DotNameLib& operator= (DotNameLib&& other) noexcept {
67+
if (this != &other) {
68+
assetsPath_ = std::move (other.assetsPath_);
69+
isInitialized_ = other.isInitialized_;
70+
other.isInitialized_ = false;
71+
logger_->infoStream () << libName_ << " move assigned";
3372
}
34-
[[nodiscard]] const std::filesystem::path& getAssetsPath () const noexcept {
35-
return assetsPath_;
73+
return *this;
74+
}
75+
76+
~DotNameLib () {
77+
if (isInitialized_) {
78+
logger_->infoStream () << libName_ << " destructed";
79+
} else {
80+
logger_->infoStream () << libName_ << " (not initialized) destructed";
3681
}
37-
};
82+
}
3883

39-
} // namespace v1
40-
} // namespace dotname
84+
// Public interface
85+
[[nodiscard]] bool isInitialized () const noexcept {
86+
return isInitialized_;
87+
}
88+
[[nodiscard]] const std::filesystem::path& getAssetsPath () const noexcept {
89+
return assetsPath_;
90+
}
91+
};
92+
} // namespace dotnamecpp::v1
4193

42-
// MIT License Copyright (c) 2024-2025 Tomáš Mark
94+
// MIT License Copyright (c) 2024-2025 Tomáš Mark

src/DotNameLib.cpp

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1 @@
11
#include <DotNameLib/DotNameLib.hpp>
2-
#include <Assets/AssetContext.hpp>
3-
#include <Logger/Logger.hpp>
4-
#include <Utils/Utils.hpp>
5-
#include <fstream>
6-
7-
#if defined(__EMSCRIPTEN__)
8-
#include <emscripten/emscripten.h>
9-
#endif
10-
11-
namespace dotname {
12-
inline namespace v1 {
13-
14-
DotNameLib::DotNameLib () : isInitialized_ (false) {
15-
LOG_D_STREAM << libName_ << " constructed ..." << "\n";
16-
}
17-
18-
DotNameLib::DotNameLib (const std::filesystem::path& assetsPath) : DotNameLib () {
19-
if (assetsPath.empty ()) {
20-
LOG_W_STREAM << "Empty assets path provided" << "\n";
21-
return;
22-
}
23-
24-
try {
25-
assetsPath_ = assetsPath;
26-
AssetContext::setAssetsPath (assetsPath);
27-
LOG_D_STREAM << "Assets: " << AssetContext::getAssetsPath () << "\n";
28-
LOG_I_STREAM << DotNameUtils::json::getCustomStringSign () << "\n";
29-
30-
// Check if logo file exists before trying to open it
31-
const auto logoPath = AssetContext::getAssetsPath () / "DotNameCppLogo.svg";
32-
LOG_D_STREAM << "Logo path: " << logoPath << "\n";
33-
34-
if (std::filesystem::exists (logoPath)) {
35-
std::ifstream logoFile (logoPath);
36-
if (logoFile.is_open ()) {
37-
LOG_D_STREAM << "Logo file successfully opened" << "\n";
38-
isInitialized_ = true;
39-
} else {
40-
LOG_W_STREAM << "Could not open logo file: " << logoPath << "\n";
41-
}
42-
} else {
43-
LOG_W_STREAM << "Logo file does not exist: " << logoPath << "\n";
44-
}
45-
} catch (const std::exception& e) {
46-
LOG_E_STREAM << "Error initializing DotNameLib: " << e.what () << "\n";
47-
isInitialized_ = false;
48-
}
49-
}
50-
51-
// Move constructor
52-
DotNameLib::DotNameLib (DotNameLib&& other) noexcept
53-
: assetsPath_ (std::move (other.assetsPath_)),
54-
isInitialized_ (other.isInitialized_) {
55-
other.isInitialized_ = false;
56-
LOG_D_STREAM << libName_ << " move constructed" << "\n";
57-
}
58-
59-
// Move assignment
60-
DotNameLib& DotNameLib::operator= (DotNameLib&& other) noexcept {
61-
if (this != &other) {
62-
assetsPath_ = std::move (other.assetsPath_);
63-
isInitialized_ = other.isInitialized_;
64-
other.isInitialized_ = false;
65-
LOG_D_STREAM << libName_ << " move assigned" << "\n";
66-
}
67-
return *this;
68-
}
69-
70-
DotNameLib::~DotNameLib () {
71-
if (isInitialized_) {
72-
LOG_D_STREAM << libName_ << " (initialized) ... destructed" << "\n";
73-
} else {
74-
LOG_D_STREAM << libName_ << " (uninitialized) ... destructed" << "\n";
75-
}
76-
}
77-
78-
} // namespace v1
79-
} // namespace dotname
80-
81-
// MIT License Copyright (c) 2024-2025 Tomáš Mark

src/Logger/ConsoleLogger.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include "ConsoleLogger.hpp"
2+
3+
// All implementations are inline in ConsoleLogger.hpp
4+
// This file is kept for compatibility with build system

0 commit comments

Comments
 (0)