Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/ConsoleJsObject.cpp

Issue 10524054: Rename ErrorCallback into LogSystem, provide a proper console API (Closed)
Patch Set: Created May 10, 2013, 2:01 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <AdblockPlus/JsValue.h> 18 #include <AdblockPlus/JsValue.h>
19 #include <AdblockPlus/ErrorCallback.h> 19 #include <AdblockPlus/LogSystem.h>
20 #include <sstream> 20 #include <sstream>
21 21
22 #include "ConsoleJsObject.h" 22 #include "ConsoleJsObject.h"
23 #include "Utils.h"
23 24
24 namespace 25 namespace
25 { 26 {
26 v8::Handle<v8::Value> ErrorCallback(const v8::Arguments& arguments) 27 v8::Handle<v8::Value> DoLog(AdblockPlus::LogSystem::LogLevel logLevel,
28 const v8::Arguments& arguments)
27 { 29 {
28 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 30 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
29 const AdblockPlus::JsEngine::Context context(jsEngine); 31 const AdblockPlus::JsEngine::Context context(jsEngine);
30 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); 32 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
31 33
32 std::stringstream message; 34 std::stringstream message;
33 for (size_t i = 0; i < converted.size(); i++) 35 for (size_t i = 0; i < converted.size(); i++)
36 {
37 if (i > 0)
38 message << " ";
34 message << converted[i]->AsString(); 39 message << converted[i]->AsString();
40 }
35 41
36 AdblockPlus::ErrorCallbackPtr callback = jsEngine->GetErrorCallback(); 42 std::stringstream source;
37 (*callback)(message.str()); 43 v8::Local<v8::StackFrame> frame = v8::StackTrace::CurrentStackTrace(1)->GetF rame(0);
44 source << AdblockPlus::Utils::FromV8String(frame->GetScriptName());
45 source << ":" << frame->GetLineNumber();
46
47 AdblockPlus::LogSystemPtr callback = jsEngine->GetLogSystem();
48 (*callback)(logLevel, message.str(), source.str());
38 return v8::Undefined(); 49 return v8::Undefined();
39 } 50 }
40 51
52 v8::Handle<v8::Value> LogCallback(const v8::Arguments& arguments)
53 {
54 return DoLog(AdblockPlus::LogSystem::LOG, arguments);
55 }
56
57 v8::Handle<v8::Value> DebugCallback(const v8::Arguments& arguments)
58 {
59 return DoLog(AdblockPlus::LogSystem::LOG, arguments);
60 }
61
62 v8::Handle<v8::Value> InfoCallback(const v8::Arguments& arguments)
63 {
64 return DoLog(AdblockPlus::LogSystem::INFO, arguments);
65 }
66
67 v8::Handle<v8::Value> WarnCallback(const v8::Arguments& arguments)
68 {
69 return DoLog(AdblockPlus::LogSystem::WARN, arguments);
70 }
71
72 v8::Handle<v8::Value> ErrorCallback(const v8::Arguments& arguments)
73 {
74 return DoLog(AdblockPlus::LogSystem::ERROR, arguments);
75 }
76
41 v8::Handle<v8::Value> TraceCallback(const v8::Arguments& arguments) 77 v8::Handle<v8::Value> TraceCallback(const v8::Arguments& arguments)
42 { 78 {
79 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
80 const AdblockPlus::JsEngine::Context context(jsEngine);
81 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
82
83 std::stringstream traceback;
84 v8::Local<v8::StackTrace> frames = v8::StackTrace::CurrentStackTrace(100);
85 for (int i = 0, l = frames->GetFrameCount(); i < l; i++)
86 {
87 v8::Local<v8::StackFrame> frame = frames->GetFrame(i);
88 traceback << (i + 1) << ": ";
89 std::string name = AdblockPlus::Utils::FromV8String(frame->GetFunctionName ());
90 if (name.size())
91 traceback << name;
92 else
93 traceback << "/* anonymous */";
94 traceback << "() at ";
95 traceback << AdblockPlus::Utils::FromV8String(frame->GetScriptName());
96 traceback << ":" << frame->GetLineNumber();
97 traceback << std::endl;
98 }
99
100 AdblockPlus::LogSystemPtr callback = jsEngine->GetLogSystem();
101 (*callback)(AdblockPlus::LogSystem::TRACE, traceback.str(), "");
43 return v8::Undefined(); 102 return v8::Undefined();
44 } 103 }
45 } 104 }
46 105
47 AdblockPlus::JsValuePtr AdblockPlus::ConsoleJsObject::Setup( 106 AdblockPlus::JsValuePtr AdblockPlus::ConsoleJsObject::Setup(
48 AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValuePtr obj) 107 AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValuePtr obj)
49 { 108 {
109 obj->SetProperty("log", jsEngine->NewCallback(::LogCallback));
110 obj->SetProperty("debug", jsEngine->NewCallback(::DebugCallback));
111 obj->SetProperty("info", jsEngine->NewCallback(::InfoCallback));
112 obj->SetProperty("warn", jsEngine->NewCallback(::WarnCallback));
50 obj->SetProperty("error", jsEngine->NewCallback(::ErrorCallback)); 113 obj->SetProperty("error", jsEngine->NewCallback(::ErrorCallback));
51 obj->SetProperty("trace", jsEngine->NewCallback(::TraceCallback)); 114 obj->SetProperty("trace", jsEngine->NewCallback(::TraceCallback));
52 return obj; 115 return obj;
53 } 116 }
OLDNEW

Powered by Google App Engine
This is Rietveld