RigsofRods
Soft-body Physics Simulation
Console.cpp
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2005-2012 Pierre-Michel Ricordel
4  Copyright 2007-2012 Thomas Fischer
5  Copyright 2013-2019 Petr Ohlidal
6 
7  For more information, see http://www.rigsofrods.org/
8 
9  Rigs of Rods is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License version 3, as
11  published by the Free Software Foundation.
12 
13  Rigs of Rods is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "Console.h"
23 
24 #include "Application.h"
25 #include "Utils.h"
26 
27 #include <Ogre.h>
28 
29 using namespace RoR;
30 using namespace Ogre;
31 
32 void Console::messageLogged(const Ogre::String& message, Ogre::LogMessageLevel lml, bool maskDebug, const Ogre::String& logName, bool& skipThisMessage)
33 {
34  if (App::diag_log_console_echo->getBool())
35  {
36  this->forwardLogMessage(CONSOLE_MSGTYPE_LOG, message, lml);
37  }
38 }
39 
40 void Console::forwardLogMessage(MessageArea area, std::string const& message, Ogre::LogMessageLevel lml)
41 {
42  switch (lml)
43  {
44  case Ogre::LML_WARNING:
45  this->putMessage(area, CONSOLE_SYSTEM_WARNING, SanitizeUtf8String(message));
46  break;
47 
48  case Ogre::LML_CRITICAL:
49  this->putMessage(area, CONSOLE_SYSTEM_ERROR, SanitizeUtf8String(message));
50  break;
51 
52  default: // LML_NORMAL, LML_TRIVIAL
53  this->putMessage(area, CONSOLE_SYSTEM_NOTICE, SanitizeUtf8String(message));
54  break;
55  }
56 }
57 
59 {
60  std::lock_guard<std::mutex> lock(m_messages_mutex); // Scoped lock
61  m_messages.erase(std::remove_if(m_messages.begin(), m_messages.end(), [user_id](const Console::Message& msg) { return msg.cm_net_userid == user_id; }), m_messages.end());
62 }
63 
64 void Console::handleMessage(MessageArea area, MessageType type, std::string const& msg, int net_userid/* = 0*/, std::string icon)
65 {
66  if (net_userid < 0) // 0=server, positive=clients, negative=invalid
67  {
68  net_userid = 0;
69  }
70 
71  // Log message to file
72  if (area != MessageArea::CONSOLE_MSGTYPE_LOG && // Don't duplicate echoed log messages
73  type != MessageType::CONSOLE_SYSTEM_NETCHAT) // Privacy
74  {
75  Str<2000> txt;
76  txt << "[RoR|";
77  switch (area)
78  {
79  case MessageArea::CONSOLE_MSGTYPE_INFO: txt << "General"; break;
80  case MessageArea::CONSOLE_MSGTYPE_SCRIPT: txt << "Script"; break;
81  case MessageArea::CONSOLE_MSGTYPE_ACTOR: txt << "Actor"; break;
82  case MessageArea::CONSOLE_MSGTYPE_TERRN: txt << "Terrn"; break;
83  default:;
84  }
85  txt << "|";
86  switch (type)
87  {
88  case MessageType::CONSOLE_SYSTEM_NOTICE: txt << "Notice"; break;
89  case MessageType::CONSOLE_SYSTEM_ERROR: txt << "Error"; break;
90  case MessageType::CONSOLE_SYSTEM_WARNING: txt << "Warning"; break;
91  case MessageType::CONSOLE_SYSTEM_REPLY: txt << "Success"; break;
92  default:;
93  }
94  txt << "] " << msg;
95  Log(txt.ToCStr());
96  }
97 
98  // Lock and update message list
99  std::lock_guard<std::mutex> lock(m_messages_mutex); // Scoped lock
100  m_messages.emplace_back(area, type, msg, this->queryMessageTimer(), net_userid, icon);
101 }
102 
103 void Console::putMessage(MessageArea area, MessageType type, std::string const& msg, std::string icon)
104 {
105  this->handleMessage(area, type, msg, 0, icon);
106 }
107 
108 void Console::putNetMessage(int user_id, MessageType type, const char* text)
109 {
110  this->handleMessage(CONSOLE_MSGTYPE_INFO, type, text, user_id);
111 }
RoR::SanitizeUtf8String
std::string SanitizeUtf8String(std::string const &str_in)
Definition: Utils.cpp:120
Console.h
RoR::Console::putMessage
void putMessage(MessageArea area, MessageType type, std::string const &msg, std::string icon="")
Definition: Console.cpp:103
Utils.h
RoR::Console::purgeNetChatMessagesByUser
void purgeNetChatMessagesByUser(int user_id)
Definition: Console.cpp:58
RoR::Console::Message
Definition: Console.h:67
RoR::Str
Wrapper for classic c-string (local buffer) Refresher: strlen() excludes '\0' terminator; strncat() A...
Definition: Str.h:35
RoR::Console::forwardLogMessage
void forwardLogMessage(MessageArea area, std::string const &msg, Ogre::LogMessageLevel lml)
Definition: Console.cpp:40
RoR::Str::ToCStr
const char * ToCStr() const
Definition: Str.h:46
RoR::Console::MessageType
MessageType
Definition: Console.h:46
Application.h
Central state/object manager and communications hub.
RoR::Console::handleMessage
void handleMessage(MessageArea area, MessageType type, std::string const &msg, int net_id=0, std::string icon="")
Definition: Console.cpp:64
RoR::App::diag_log_console_echo
CVar * diag_log_console_echo
Definition: Application.cpp:146
RoR::Console::messageLogged
virtual void messageLogged(const Ogre::String &message, Ogre::LogMessageLevel lml, bool maskDebug, const Ogre::String &logName, bool &skipThisMessage) override
Definition: Console.cpp:32
RoR::Console::putNetMessage
void putNetMessage(int user_id, MessageType type, const char *text)
Definition: Console.cpp:108
Ogre
Definition: ExtinguishableFireAffector.cpp:35
RoR
Definition: AppContext.h:36
RoR::Log
void Log(const char *msg)
The ultimate, application-wide logging function. Adds a line (any length) in 'RoR....
Definition: Application.cpp:423
RoR::Console::MessageArea
MessageArea
Definition: Console.h:58