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 {
28  public:
29  SerialLogger();
30  virtual ~SerialLogger();
31 
32  virtual void callback(const LogCord &cord);
33 
34  private:
35  Serial *m_pSerial;
36  bool m_bInitialised;
37  Spinlock m_Lock;
38 };
39 
40 static SerialLogger g_SerialCallback;
41 
42 void installSerialLogger()
43 {
44  Log::instance().installCallback(&g_SerialCallback, false);
45 }
46 
47 SerialLogger::SerialLogger()
48  : m_pSerial(nullptr), m_bInitialised(false), m_Lock(false)
49 {
50 }
51 SerialLogger::~SerialLogger() = default;
52 
53 void SerialLogger::callback(const LogCord &cord)
54 {
55  if (!m_bInitialised)
56  {
57  m_bInitialised = Machine::instance().isInitialised();
58  if (!m_bInitialised)
59  {
60  return;
61  }
62  else
63  {
64  m_pSerial = Machine::instance().getSerial(0);
65  }
66  }
67 
68  m_Lock.acquire();
69  for (auto it : cord)
70  {
71  m_pSerial->write(it);
72  }
73 #ifndef SERIAL_IS_FILE
74  // Handle carriage return if we're writing to a real terminal
75  // Technically this will create a \n\r, but it will do the same
76  // thing. This may also be redundant, but better to be safe than
77  // sorry imho.
78  m_pSerial->write('\r');
79 #endif
80  m_Lock.release();
81 }
void release()
Definition: Spinlock.cc:273
virtual Serial * getSerial(size_t n)=0
bool acquire(bool recurse=false, bool safe=true)
Definition: Spinlock.cc:43
static EXPORTED_PUBLIC Log & instance()
Definition: Log.cc:108
EXPORTED_PUBLIC void installCallback(LogCallback *pCallback, bool bSkipBacklog=false)
Definition: Log.cc:146