diff options
| author | Carl Hetherington <cth@carlh.net> | 2025-05-30 00:51:57 +0200 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2025-05-30 00:51:57 +0200 |
| commit | 437a0f28eac4464ffec24c83c0b6a767495a415e (patch) | |
| tree | 3fbe6f48ce9409d392bda6e1512ee9475af6710c /src | |
| parent | 047eb588b0901cf79be55a3421697a0b844fdf86 (diff) | |
Cleanup: collapse three logging classes into one.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/encode_cli.cc | 2 | ||||
| -rw-r--r-- | src/lib/grok/context.cc | 24 | ||||
| -rw-r--r-- | src/lib/grok/context.h | 47 | ||||
| -rw-r--r-- | src/lib/grok/message.cc | 3 | ||||
| -rw-r--r-- | src/lib/grok/messenger.cc | 11 | ||||
| -rw-r--r-- | src/lib/grok/messenger.h | 57 | ||||
| -rw-r--r-- | src/lib/grok/shm.cc | 21 | ||||
| -rw-r--r-- | src/lib/grok/synch.cc | 17 | ||||
| -rw-r--r-- | src/lib/j2k_encoder.cc | 27 | ||||
| -rw-r--r-- | src/tools/dcpomatic.cc | 2 | ||||
| -rw-r--r-- | src/tools/dcpomatic_batch.cc | 2 | ||||
| -rw-r--r-- | src/tools/dcpomatic_server.cc | 2 | ||||
| -rw-r--r-- | src/tools/dcpomatic_server_cli.cc | 2 |
13 files changed, 91 insertions, 126 deletions
diff --git a/src/lib/encode_cli.cc b/src/lib/encode_cli.cc index fa304b6e0..fddfe0621 100644 --- a/src/lib/encode_cli.cc +++ b/src/lib/encode_cli.cc @@ -576,7 +576,7 @@ encode_cli(int argc, char* argv[], function<void (string)> out, function<void () } #ifdef DCPOMATIC_GROK - grk_plugin::setMessengerLogger(new grk_plugin::GrokLogger("[GROK] ")); + grk_plugin::setGrokLogger(new grk_plugin::GrokLogger("[GROK] ")); setup_grok_library_path(); #endif diff --git a/src/lib/grok/context.cc b/src/lib/grok/context.cc index 91ab26600..9d4b17e9c 100644 --- a/src/lib/grok/context.cc +++ b/src/lib/grok/context.cc @@ -39,7 +39,7 @@ GrokContext::GrokContext(DcpomaticContext* dcpomatic_context) boost::filesystem::path folder(_dcpomatic_context->location); boost::filesystem::path binary_path = folder / "grk_compress"; if (!boost::filesystem::exists(binary_path)) { - getMessengerLogger()->error( + getGrokLogger()->error( "Invalid binary location %s", _dcpomatic_context->location.c_str() ); return; @@ -78,7 +78,7 @@ GrokContext::GrokContext(DcpomaticContext* dcpomatic_context) } } } catch (std::exception& ex) { - getMessengerLogger()->error("%s",ex.what()); + getGrokLogger()->error("%s",ex.what()); } }; @@ -134,7 +134,7 @@ GrokContext::launch(DCPVideo dcpv, int device) } if (!fs::exists(_dcpomatic_context->location) || !fs::is_directory(_dcpomatic_context->location)) { - getMessengerLogger()->error("Invalid directory %s", _dcpomatic_context->location.c_str()); + getGrokLogger()->error("Invalid directory %s", _dcpomatic_context->location.c_str()); return false; } @@ -186,3 +186,21 @@ GrokContext::frame_done() _dcpomatic_context->history.event(); } + +GrokLogger* grk_plugin::sLogger = nullptr; + + +void +grk_plugin::setGrokLogger(grk_plugin::GrokLogger* logger) +{ + delete sLogger; + sLogger = logger; +} + + +grk_plugin::GrokLogger* +grk_plugin::getGrokLogger() +{ + return sLogger; +} + diff --git a/src/lib/grok/context.h b/src/lib/grok/context.h index 405ae2352..8fa7d49af 100644 --- a/src/lib/grok/context.h +++ b/src/lib/grok/context.h @@ -35,31 +35,58 @@ namespace grk_plugin { -struct GrokLogger : public MessengerLogger { - explicit GrokLogger(const std::string &preamble) : MessengerLogger(preamble) - {} - virtual ~GrokLogger() = default; - void info(const char* fmt, ...) override{ +struct GrokLogger +{ + explicit GrokLogger(std::string const& preamble) + : _preamble(preamble) + { + + } + + void info(char const* fmt, ...) + { va_list arg; va_start(arg, fmt); - dcpomatic_log->log(preamble_ + log_message(fmt, arg),LogEntry::TYPE_GENERAL); + dcpomatic_log->log(_preamble + log_message(fmt, arg),LogEntry::TYPE_GENERAL); va_end(arg); } - void warn(const char* fmt, ...) override{ + + void warn(char const* fmt, ...) + { va_list arg; va_start(arg, fmt); - dcpomatic_log->log(preamble_ + log_message(fmt, arg),LogEntry::TYPE_WARNING); + dcpomatic_log->log(_preamble + log_message(fmt, arg),LogEntry::TYPE_WARNING); va_end(arg); } - void error(const char* fmt, ...) override{ + + void error(char const* fmt, ...) + { va_list arg; va_start(arg, fmt); - dcpomatic_log->log(preamble_ + log_message(fmt, arg),LogEntry::TYPE_ERROR); + dcpomatic_log->log(_preamble + log_message(fmt, arg),LogEntry::TYPE_ERROR); va_end(arg); } + +private: + template<typename... Args> + std::string log_message(char const* const format, Args&... args) noexcept + { + constexpr size_t message_size = 512; + char message[message_size]; + + std::snprintf(message, message_size, format, args...); + return std::string(message); + } + + std::string _preamble; }; +extern GrokLogger* sLogger; +void setGrokLogger(GrokLogger* logger); +GrokLogger* getGrokLogger(); + + struct DcpomaticContext { DcpomaticContext( diff --git a/src/lib/grok/message.cc b/src/lib/grok/message.cc index 296d45f6b..054bbf3a3 100644 --- a/src/lib/grok/message.cc +++ b/src/lib/grok/message.cc @@ -19,6 +19,7 @@ */ +#include "context.h" #include "messenger.h" #include <string> @@ -41,7 +42,7 @@ string Message::next() { if (_ct == _cs.size()) { - getMessengerLogger()->error("Message: comma separated list exhausted. returning empty."); + getGrokLogger()->error("Message: comma separated list exhausted. returning empty."); return ""; } return _cs[_ct++]; diff --git a/src/lib/grok/messenger.cc b/src/lib/grok/messenger.cc index ce267759a..5f900c9a7 100644 --- a/src/lib/grok/messenger.cc +++ b/src/lib/grok/messenger.cc @@ -19,6 +19,7 @@ */ +#include "context.h" #include "shm.h" #include "synch.h" #include "messenger.h" @@ -313,7 +314,7 @@ Messenger::waitForClientInit() } auto status = _async_result.wait_for(std::chrono::milliseconds(100)); if (status == std::future_status::ready) { - getMessengerLogger()->error("Grok exited unexpectedly during initialization"); + getGrokLogger()->error("Grok exited unexpectedly during initialization"); return false; } } @@ -371,12 +372,12 @@ Messenger::launch(std::string const& cmd, boost::filesystem::path const& dir) { // Execute the command using std::async and std::system auto const cmd_with_dir = dir / cmd; - getMessengerLogger()->info(cmd_with_dir.string().c_str()); + getGrokLogger()->info(cmd_with_dir.string().c_str()); _async_result = std::async(std::launch::async, [cmd_with_dir]() { return std::system(cmd_with_dir.string().c_str()); }); auto const success = _async_result.valid(); if (!success) { - getMessengerLogger()->error("Grok launch failed"); + getGrokLogger()->error("Grok launch failed"); } return success; @@ -443,9 +444,9 @@ Messenger::shutdown() send(GRK_MSGR_BATCH_SHUTDOWN); int result = _async_result.get(); if(result != 0) { - getMessengerLogger()->error("Accelerator failed with return code: %d\n",result); + getGrokLogger()->error("Accelerator failed with return code: %d\n",result); } } catch (std::exception &ex) { - getMessengerLogger()->error("%s",ex.what()); + getGrokLogger()->error("%s",ex.what()); } } diff --git a/src/lib/grok/messenger.h b/src/lib/grok/messenger.h index 15a539299..6e29c008d 100644 --- a/src/lib/grok/messenger.h +++ b/src/lib/grok/messenger.h @@ -50,63 +50,6 @@ static const std::string GRK_MSGR_BATCH_FLUSH = "GRK_MSGR_BATCH_FLUSH"; static const size_t messageBufferLen = 256; -struct MessengerLogger -{ - explicit MessengerLogger(const std::string &preamble) - : preamble_(preamble) - { - - } - - virtual ~MessengerLogger() = default; - - virtual void info(const char* fmt, ...) - { - va_list args; - std::string new_fmt = preamble_ + fmt + "\n"; - va_start(args, fmt); - vfprintf(stdout, new_fmt.c_str(), args); - va_end(args); - } - - virtual void warn(const char* fmt, ...) - { - va_list args; - std::string new_fmt = preamble_ + fmt + "\n"; - va_start(args, fmt); - vfprintf(stdout, new_fmt.c_str(), args); - va_end(args); - } - - virtual void error(const char* fmt, ...) - { - va_list args; - std::string new_fmt = preamble_ + fmt + "\n"; - va_start(args, fmt); - vfprintf(stderr, new_fmt.c_str(), args); - va_end(args); - } - -protected: - template<typename... Args> - std::string log_message(char const* const format, Args&... args) noexcept - { - constexpr size_t message_size = 512; - char message[message_size]; - - std::snprintf(message, message_size, format, args...); - return std::string(message); - } - - std::string preamble_; -}; - - -extern MessengerLogger* sLogger; -void setMessengerLogger(MessengerLogger* logger); -MessengerLogger* getMessengerLogger(); - - template<typename Data> class MessengerBlockingQueue { diff --git a/src/lib/grok/shm.cc b/src/lib/grok/shm.cc index 41fe8169a..938b61834 100644 --- a/src/lib/grok/shm.cc +++ b/src/lib/grok/shm.cc @@ -19,6 +19,7 @@ */ +#include "context.h" #include "messenger.h" #include "shm.h" @@ -29,20 +30,20 @@ grk_plugin::init_shm(char const* name, size_t len, int* shm_fd, char** buffer) *shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666); if (*shm_fd < 0) { - getMessengerLogger()->error("Error opening shared memory: %s", strerror(errno)); + getGrokLogger()->error("Error opening shared memory: %s", strerror(errno)); return false; } int rc = ftruncate(*shm_fd, sizeof(char) * len); if (rc) { - getMessengerLogger()->error("Error truncating shared memory: %s", strerror(errno)); + getGrokLogger()->error("Error truncating shared memory: %s", strerror(errno)); if (close(*shm_fd)) { - getMessengerLogger()->error("Error closing shared memory: %s", strerror(errno)); + getGrokLogger()->error("Error closing shared memory: %s", strerror(errno)); } rc = shm_unlink(name); if (rc && errno != ENOENT) { - getMessengerLogger()->error("Error unlinking shared memory: %s", strerror(errno)); + getGrokLogger()->error("Error unlinking shared memory: %s", strerror(errno)); } return false; } @@ -50,13 +51,13 @@ grk_plugin::init_shm(char const* name, size_t len, int* shm_fd, char** buffer) *buffer = static_cast<char*>(mmap(0, len, PROT_WRITE, MAP_SHARED, *shm_fd, 0)); if (!*buffer) { - getMessengerLogger()->error("Error mapping shared memory: %s", strerror(errno)); + getGrokLogger()->error("Error mapping shared memory: %s", strerror(errno)); if (close(*shm_fd)) { - getMessengerLogger()->error("Error closing shared memory: %s", strerror(errno)); + getGrokLogger()->error("Error closing shared memory: %s", strerror(errno)); } rc = shm_unlink(name); if (rc && errno != ENOENT) { - getMessengerLogger()->error("Error unlinking shared memory: %s", strerror(errno)); + getGrokLogger()->error("Error unlinking shared memory: %s", strerror(errno)); } } @@ -75,20 +76,20 @@ grk_plugin::deinit_shm(char const* name, size_t len, int &shm_fd, char** buffer) *buffer = nullptr; if (rc) { - getMessengerLogger()->error("Error unmapping shared memory %s: %s", name, strerror(errno)); + getGrokLogger()->error("Error unmapping shared memory %s: %s", name, strerror(errno)); } rc = close(shm_fd); shm_fd = 0; if (rc) { - getMessengerLogger()->error("Error closing shared memory %s: %s", name, strerror(errno)); + getGrokLogger()->error("Error closing shared memory %s: %s", name, strerror(errno)); } rc = shm_unlink(name); if (rc && errno != ENOENT) { - getMessengerLogger()->error("Error unlinking shared memory %s : %s", name, strerror(errno)); + getGrokLogger()->error("Error unlinking shared memory %s : %s", name, strerror(errno)); } return true; diff --git a/src/lib/grok/synch.cc b/src/lib/grok/synch.cc index ad3103e98..3296e3a9f 100644 --- a/src/lib/grok/synch.cc +++ b/src/lib/grok/synch.cc @@ -19,6 +19,7 @@ */ +#include "context.h" #include "messenger.h" #include "synch.h" @@ -49,7 +50,7 @@ Synch::post(SynchDirection dir) { auto sem = dir == SYNCH_SENT ? _sent_sem : _receive_ready_sem; if (sem_post(sem)) { - getMessengerLogger()->error("Error posting to semaphore: %s", strerror(errno)); + getGrokLogger()->error("Error posting to semaphore: %s", strerror(errno)); } } @@ -59,7 +60,7 @@ Synch::wait(SynchDirection dir) { auto sem = dir == SYNCH_SENT ? _sent_sem : _receive_ready_sem; if (sem_wait(sem)) { - getMessengerLogger()->error("Error waiting for semaphore: %s", strerror(errno)); + getGrokLogger()->error("Error waiting for semaphore: %s", strerror(errno)); } } @@ -69,12 +70,12 @@ Synch::open() { _sent_sem = sem_open(_sent_sem_name.c_str(), O_CREAT, 0666, 0); if (!_sent_sem) { - getMessengerLogger()->error("Error opening shared memory: %s", strerror(errno)); + getGrokLogger()->error("Error opening shared memory: %s", strerror(errno)); } _receive_ready_sem = sem_open(_receive_ready_sem_name.c_str(), O_CREAT, 0666, 1); if (!_receive_ready_sem) { - getMessengerLogger()->error("Error opening shared memory: %s", strerror(errno)); + getGrokLogger()->error("Error opening shared memory: %s", strerror(errno)); } } @@ -83,11 +84,11 @@ void Synch::close() { if (sem_close(_sent_sem)) { - getMessengerLogger()->error("Error closing semaphore %s: %s", _sent_sem_name.c_str(), strerror(errno)); + getGrokLogger()->error("Error closing semaphore %s: %s", _sent_sem_name.c_str(), strerror(errno)); } if (sem_close(_receive_ready_sem)) { - getMessengerLogger()->error("Error closing semaphore %s: %s", _receive_ready_sem_name.c_str(), strerror(errno)); + getGrokLogger()->error("Error closing semaphore %s: %s", _receive_ready_sem_name.c_str(), strerror(errno)); } } @@ -97,11 +98,11 @@ Synch::unlink() { int rc = sem_unlink(_sent_sem_name.c_str()); if (rc == -1 && errno != ENOENT) { - getMessengerLogger()->error("Error unlinking semaphore %s: %s", _sent_sem_name.c_str(), strerror(errno)); + getGrokLogger()->error("Error unlinking semaphore %s: %s", _sent_sem_name.c_str(), strerror(errno)); } rc = sem_unlink(_receive_ready_sem_name.c_str()); if (rc == -1 && errno != ENOENT) { - getMessengerLogger()->error("Error unlinking semaphore %s: %s", _receive_ready_sem_name.c_str(), strerror(errno)); + getGrokLogger()->error("Error unlinking semaphore %s: %s", _receive_ready_sem_name.c_str(), strerror(errno)); } } diff --git a/src/lib/j2k_encoder.cc b/src/lib/j2k_encoder.cc index 73ec5bd08..802193437 100644 --- a/src/lib/j2k_encoder.cc +++ b/src/lib/j2k_encoder.cc @@ -60,33 +60,6 @@ using boost::optional; using dcp::Data; using namespace dcpomatic; -#ifdef DCPOMATIC_GROK - -namespace grk_plugin { - -MessengerLogger* sLogger = nullptr; - -#if defined(__GNUC__) || defined(__clang__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunused-function" -#endif -void setMessengerLogger(grk_plugin::MessengerLogger* logger) -{ - delete sLogger; - sLogger = logger; -} -#if defined(__GNUC__) || defined(__clang__) -#pragma GCC diagnostic pop -#endif -grk_plugin::MessengerLogger* getMessengerLogger() -{ - return sLogger; -} - -} - -#endif - /** @param film Film that we are encoding. * @param writer Writer that we are using. diff --git a/src/tools/dcpomatic.cc b/src/tools/dcpomatic.cc index 4ab9c03ed..dc823cf7e 100644 --- a/src/tools/dcpomatic.cc +++ b/src/tools/dcpomatic.cc @@ -1763,7 +1763,7 @@ private: } #ifdef DCPOMATIC_GROK - grk_plugin::setMessengerLogger(new grk_plugin::GrokLogger("[GROK] ")); + grk_plugin::setGrokLogger(new grk_plugin::GrokLogger("[GROK] ")); setup_grok_library_path(); #endif } diff --git a/src/tools/dcpomatic_batch.cc b/src/tools/dcpomatic_batch.cc index c6a58f741..5954ebf00 100644 --- a/src/tools/dcpomatic_batch.cc +++ b/src/tools/dcpomatic_batch.cc @@ -501,7 +501,7 @@ class App : public wxApp } #ifdef DCPOMATIC_GROK - grk_plugin::setMessengerLogger(new grk_plugin::GrokLogger("[GROK] ")); + grk_plugin::setGrokLogger(new grk_plugin::GrokLogger("[GROK] ")); setup_grok_library_path(); #endif diff --git a/src/tools/dcpomatic_server.cc b/src/tools/dcpomatic_server.cc index e5d8b0a90..f82491c89 100644 --- a/src/tools/dcpomatic_server.cc +++ b/src/tools/dcpomatic_server.cc @@ -337,7 +337,7 @@ private: SetExitOnFrameDelete (false); #ifdef DCPOMATIC_GROK - grk_plugin::setMessengerLogger(new grk_plugin::GrokLogger("[GROK] ")); + grk_plugin::setGrokLogger(new grk_plugin::GrokLogger("[GROK] ")); setup_grok_library_path(); #endif diff --git a/src/tools/dcpomatic_server_cli.cc b/src/tools/dcpomatic_server_cli.cc index ea78d41a5..44da5fbef 100644 --- a/src/tools/dcpomatic_server_cli.cc +++ b/src/tools/dcpomatic_server_cli.cc @@ -117,7 +117,7 @@ main (int argc, char* argv[]) } #ifdef DCPOMATIC_GROK - setMessengerLogger(new grk_plugin::GrokLogger("[GROK] ")); + setGrokLogger(new grk_plugin::GrokLogger("[GROK] ")); setup_grok_library_path(); #endif |
