Home | History | Annotate | Download | only in Core
      1 //===-- StreamCallback.cpp -------------------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #include <stdio.h>
     11 
     12 #include "lldb/lldb-private.h"
     13 #include "lldb/Core/Broadcaster.h"
     14 #include "lldb/Core/Event.h"
     15 #include "lldb/Core/StreamCallback.h"
     16 #include "lldb/Host/Host.h"
     17 
     18 using namespace lldb;
     19 using namespace lldb_private;
     20 
     21 
     22 StreamCallback::StreamCallback (lldb::LogOutputCallback callback, void *baton) :
     23     Stream (0, 4, eByteOrderBig),
     24     m_callback (callback),
     25     m_baton (baton),
     26     m_accumulated_data (),
     27     m_collection_mutex ()
     28 {
     29 }
     30 
     31 StreamCallback::~StreamCallback ()
     32 {
     33 }
     34 
     35 StreamString &
     36 StreamCallback::FindStreamForThread(lldb::tid_t cur_tid)
     37 {
     38     Mutex::Locker locker(m_collection_mutex);
     39     collection::iterator iter = m_accumulated_data.find (cur_tid);
     40     if (iter == m_accumulated_data.end())
     41     {
     42         std::pair<collection::iterator, bool> ret;
     43         ret = m_accumulated_data.insert(std::pair<lldb::tid_t,StreamString>(cur_tid, StreamString()));
     44         iter = ret.first;
     45     }
     46     return (*iter).second;
     47 }
     48 
     49 void
     50 StreamCallback::Flush ()
     51 {
     52     lldb::tid_t cur_tid = Host::GetCurrentThreadID();
     53     StreamString &out_stream = FindStreamForThread(cur_tid);
     54     m_callback (out_stream.GetData(), m_baton);
     55     out_stream.Clear();
     56 }
     57 
     58 size_t
     59 StreamCallback::Write (const void *s, size_t length)
     60 {
     61     lldb::tid_t cur_tid = Host::GetCurrentThreadID();
     62     FindStreamForThread(cur_tid).Write (s, length);
     63     return length;
     64 }
     65