Refactor logging: reduce inlined code and write to stderr directly instead of using std::cerr.