CaptureMoment 0.1.0
Free and open-source non-destructive photo editor
Loading...
Searching...
No Matches
core_initialization.cpp
Go to the documentation of this file.
1
12#include "config/app_config.h"
13#include <spdlog/spdlog.h>
14#include <spdlog/cfg/env.h>
15#include <spdlog/sinks/stdout_color_sinks.h>
16#include <mutex>
17
18namespace CaptureMoment::Core {
19
20namespace {
21// Global flag used by std::call_once to ensure initialization runs only once.
22inline std::once_flag s_init_flag;
23
24
25// ============================================================
26// Helper: Logging Initialization
27// ============================================================
28void init_logging()
29{
30 try {
31 auto console_logger = spdlog::get("CaptureMoment");
32
33 if (!console_logger) {
34 console_logger = spdlog::stdout_color_mt("CaptureMoment");
35 if (!console_logger) {
36 std::cerr << "Failed to create spdlog logger." << std::endl;
37 return;
38 }
39 spdlog::info("CaptureMoment Logger created and registered.");
40 } else {
41 spdlog::info("CaptureMoment Logger already exists, retrieving it.");
42 }
43
44
45 // 2. Set Default Level (Can be overridden by env var or programmatic call)
46 console_logger->set_level(spdlog::level::trace);
47
48 // 3. Optional: Load log levels from environment variable (Uses SPDLOG_LEVEL by default)
49 // This will override the level set above if the env var is present.
50 spdlog::cfg::load_env_levels(); // This checks the SPDLOG_LEVEL environment variable
51
52 // 4. Register the logger and set it as the default
53 spdlog::set_default_logger(console_logger);
54
55 // 5. Confirmation log (now using the configured logger)
56 spdlog::info("CaptureMoment Logger initialized. Level: {}", spdlog::level::to_string_view(spdlog::default_logger()->level()));
57
58 } catch (const spdlog::spdlog_ex& ex) {
59 std::cerr << "Failed to initialize spdlog: " << ex.what() << std::endl;
60 }
61}
62
63// ============================================================
64// Helper: Programmatically Set Log Level
65// ============================================================
66void set_log_level(const char* level_str)
67{
68 if (!level_str) return;
69
70 // Convert input string to lowercase for case-insensitive matching
71 std::string level_str_lower = level_str;
72 std::transform(level_str_lower.begin(), level_str_lower.end(), level_str_lower.begin(), ::tolower);
73
74 // Get the current default logger
75 auto logger = spdlog::default_logger();
76 if (!logger) return;
77
78 // Map string to spdlog level enum
79 if (level_str_lower == "trace") {
80 logger->set_level(spdlog::level::trace);
81 }
82 else if (level_str_lower == "debug") {
83 logger->set_level(spdlog::level::debug);
84 }
85 else if (level_str_lower == "info") {
86 logger->set_level(spdlog::level::info);
87 }
88 else if (level_str_lower == "warn") {
89 logger->set_level(spdlog::level::warn);
90 }
91 else if (level_str_lower == "error") {
92 logger->set_level(spdlog::level::err);
93 }
94 else if (level_str_lower == "off") {
95 logger->set_level(spdlog::level::off);
96 }
97 else {
98 // If unknown string, fallback to info
99 logger->set_level(spdlog::level::info);
100 }
101}
102
103// ============================================================
104// Helper: Backends Registration
105// ============================================================
106void init_backends()
107{
108 // Call the registration function located in ImageProcessing namespace
109 // This function registers CPU_Halide and GPU_Halide factories.
111}
112
113// ============================================================
114// Helper: Backend Decider & Configuration
115// ============================================================
116void init_backend_decider()
117{
118 spdlog::info("[CoreInitialization] Starting backend selection benchmark...");
119
120 // 1. Create the decider
122
123 // 2. Run benchmark (CPU vs GPU comparison)
124 auto backend = benchmark_decider.decide();
125
126 // 3. Store the selected MemoryType in global Config
128
129 // 4. Store the specific Halide Target (Host+CUDA, Host+Vulkan, etc.) in Config
130 // This allows WorkingImageGPU_Halide to retrieve the correct target.
132
133 spdlog::info("[CoreInitialization] Backend configuration complete.");
134}
135
136// ============================================================
137// Main Initialization Logic
138// ============================================================
139void perform_initialization()
140{
141 // Step 1: Initialize Logging
142 init_logging();
143
144 // Step 2: Register Factories
145 init_backends();
146
147 // Step 3: Run Benchmark Decider & Setup Config
148 init_backend_decider();
149}
150
151}
152
154{
155 // std::call_once ensures that 'perform_initialization' is executed exactly once.
156 // It is thread-safe: if multiple threads call initialize(), they will synchronize,
157 // and only one will execute the registration and benchmarking.
158 std::call_once(s_init_flag, perform_initialization);
159}
160
161} // namespace CaptureMoment::Core
Application-wide configuration singleton.
Concrete implementation of IBackendDecider that chooses backend based on performance benchmarks.
static void setHalideTarget(const Halide::Target &target)
Sets the configured Halide Target object.
static AppConfig & instance()
Gets the singleton instance of AppConfig.
void setProcessingBackend(Common::MemoryType backend)
Sets the processing backend (CPU or GPU) for image operations.
Implements IBackendDecider using a runtime performance comparison strategy.
Common::MemoryType decide() override
Decides the optimal memory type (CPU or GPU) by running benchmarks.
const Halide::Target & getWinningTarget() const
Gets the winning Halide::Target object selected by decide().
Core library initialization entry point.
void registerDefaultBackends()
Registers all default Core backends (CPU and GPU) to the factory.
Manages the lifecycle of a single IWorkingImage instance.
void initialize()
Initializes the CaptureMoment Core library.
void set_log_level(const char *level_str)
Declaration of backend registration function.
CaptureMoment — footer