The Pedigree Project 0.1
SerialLog.cc
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include "pedigree/kernel/Log.h"
21#include "pedigree/kernel/Spinlock.h"
22#include "pedigree/kernel/machine/Machine.h"
23#include "pedigree/kernel/machine/Serial.h"
24#include "pedigree/kernel/utilities/Cord.h"
25
27 public:
29 virtual ~SerialLogger();
30
31 virtual void callback(const LogCord& cord, bool locked = true);
32
33 private:
34 Serial* m_pSerial;
35 bool m_bInitialised;
36 Spinlock m_Lock;
37};
38
39static SerialLogger g_SerialCallback;
40
41void installSerialLogger() {
42 Log::instance().installCallback(&g_SerialCallback, false);
43}
44
45SerialLogger::SerialLogger() : m_pSerial(nullptr), m_bInitialised(false), m_Lock(false) {}
46
47SerialLogger::~SerialLogger() {
49}
50
51void SerialLogger::callback(const LogCord& cord, bool locked) {
52 if (!m_bInitialised) {
53 m_bInitialised = Machine::instance().isInitialised();
54 if (!m_bInitialised) {
55 return;
56 } else {
57 m_pSerial = Machine::instance().getSerial(0);
58 }
59 }
60
61 if (LIKELY(locked)) {
62 m_Lock.acquire();
63 }
64 m_pSerial->write_str(cord);
65#if !SERIAL_IS_FILE
66 // Handle carriage return if we're writing to a real terminal
67 // Technically this will create a \n\r, but it will do the same
68 // thing. This may also be redundant, but better to be safe than
69 // sorry imho.
70 m_pSerial->write('\r');
71#endif
72 if (LIKELY(locked)) {
73 m_Lock.release();
74 }
75}
EXPORTED_PUBLIC bool removeCallback(LogCallback *pCallback)
Definition Log.cc:228
static EXPORTED_PUBLIC Log & instance()
Definition Log.cc:117
EXPORTED_PUBLIC bool installCallback(LogCallback *pCallback, bool bSkipBacklog=false)
Definition Log.cc:145
virtual Serial * getSerial(size_t n)=0
void release()
Definition Spinlock.cc:161
bool acquire(bool recurse=false, bool safe=true)
Definition Spinlock.cc:35