fastcgi++
A C++ FastCGI/Web API
log.cpp
Go to the documentation of this file.
1 
10 /*******************************************************************************
11 * Copyright (C) 2016 Eddie Carle [eddie@isatec.ca] *
12 * *
13 * This file is part of fastcgi++. *
14 * *
15 * fastcgi++ is free software: you can redistribute it and/or modify it under *
16 * the terms of the GNU Lesser General Public License as published by the Free *
17 * Software Foundation, either version 3 of the License, or (at your option) *
18 * any later version. *
19 * *
20 * fastcgi++ is distributed in the hope that it will be useful, but WITHOUT ANY *
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for *
23 * more details. *
24 * *
25 * You should have received a copy of the GNU Lesser General Public License *
26 * along with fastcgi++. If not, see <http://www.gnu.org/licenses/>. *
27 *******************************************************************************/
28 
29 #include "fastcgi++/log.hpp"
30 
31 #include <iomanip>
32 #include <iostream>
33 #include <ctime>
34 #include <codecvt>
35 #include <cstring>
36 #include <array>
37 #include <sstream>
38 
39 #include <unistd.h>
40 #include <limits.h>
41 #include <sys/types.h>
42 
44 namespace Fastcgipp
45 {
47  namespace Logging
48  {
49  std::wstring getHostname()
50  {
51  char buffer[HOST_NAME_MAX+2];
52  gethostname(buffer, sizeof(buffer));
53  std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
54  try
55  {
56  return(converter.from_bytes(
57  buffer,
58  buffer+std::strlen(buffer)));
59  }
60  catch(const std::range_error& e)
61  {
62  WARNING_LOG("Error in hostname code conversion from utf8")
63  return std::wstring(L"localhost");
64  }
65 
66  }
67 
68  std::wstring getProgram()
69  {
70  std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
71  std::wostringstream ss;
72  try
73  {
74  ss << converter.from_bytes(
75  program_invocation_name,
76  program_invocation_name
77  +std::strlen(program_invocation_name));
78  }
79  catch(const std::range_error& e)
80  {
81  WARNING_LOG("Error in program name code conversion from utf8")
82  ss << "unknown";
83  }
84 
85  ss << '[' << getpid() << ']';
86  return ss.str();
87  }
88 
89  std::array<std::wstring, 6> levels
90  {{
91  L"[info]: ",
92  L"[fail]: ",
93  L"[error]: ",
94  L"[warning]: ",
95  L"[debug]: ",
96  L"[diagnostic]: "
97  }};
98  }
99 }
100 
101 std::wostream* Fastcgipp::Logging::logstream(&std::wcerr);
103 bool Fastcgipp::Logging::suppress(false);
106 
108 {
109  const std::time_t now = std::time(nullptr);
110  *logstream
111  << std::put_time(std::localtime(&now), L"%b %d %H:%M:%S ")
112  << hostname << ' ' << program << ' ' << levels[level];
113 }
Topmost namespace for the fastcgi++ library.
void header(Level level)
Send a log header to logstream.
Definition: log.cpp:107
std::mutex mutex
Thread safe the logging mechanism.
Definition: log.cpp:102
std::wstring getProgram()
Definition: log.cpp:68
bool suppress
Set to true if you want to suppress non-error logs.
std::wstring getHostname()
Definition: log.cpp:49
Level
Communicate the log level to the header generator.
Definition: log.hpp:61
std::wostream * logstream
The actual stream we will be logging to.
std::array< std::wstring, 6 > levels
Definition: log.cpp:90
std::wstring program
Application name to use in loggin.
std::wstring hostname
Hostname to use in logging.
Declares the Fastcgipp debugging/logging facilities.
#define WARNING_LOG(data)
Log any externally caused "errors".
Definition: log.hpp:124