// file      : web/apache/log -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#ifndef WEB_APACHE_LOG
#define WEB_APACHE_LOG

#include <httpd.h>    // request_rec
#include <http_log.h>

#include <cstdint>   // uint64_t
#include <algorithm> // min()

#include <web/module>

namespace web
{
  namespace apache
  {
    class log: public web::log
    {
    public:

      log (server_rec* server) noexcept: server_ (server) {}

      virtual void
      write (const char* msg) {write (APLOG_ERR, msg);}

      // Apache-specific interface.
      //
      void
      write (int level, const char* msg) const noexcept
      {
        write (nullptr, 0, nullptr, level, msg);
      }

      void
      write (const char* file,
             std::uint64_t line,
             const char* func,
             int level,
             const char* msg) const noexcept
      {
        if (file && *file)
          file = nullptr; // skip file/line placeholder from log line.

        level = std::min (level, APLOG_TRACE8);

        if (func)
          ap_log_error (file,
                        line,
                        APLOG_NO_MODULE,
                        level,
                        0,
                        server_,
                        "[%s]: %s",
                        func,
                        msg);
        else
          // skip function name placeholder from log line
          //
          ap_log_error (file,
                        line,
                        APLOG_NO_MODULE,
                        level,
                        0,
                        server_,
                        ": %s",
                        msg);
      }

    private:
      server_rec* server_;
    };
  }
}

#endif // WEB_APACHE_LOG