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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/ConsoleJsObject.cpp
===================================================================
--- a/src/ConsoleJsObject.cpp
+++ b/src/ConsoleJsObject.cpp
@@ -11,43 +11,106 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
#include <AdblockPlus/JsValue.h>
-#include <AdblockPlus/ErrorCallback.h>
+#include <AdblockPlus/LogSystem.h>
#include <sstream>
#include "ConsoleJsObject.h"
+#include "Utils.h"
namespace
{
- v8::Handle<v8::Value> ErrorCallback(const v8::Arguments& arguments)
+ v8::Handle<v8::Value> DoLog(AdblockPlus::LogSystem::LogLevel logLevel,
+ const v8::Arguments& arguments)
{
AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arguments);
const AdblockPlus::JsEngine::Context context(jsEngine);
AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
std::stringstream message;
for (size_t i = 0; i < converted.size(); i++)
+ {
+ if (i > 0)
+ message << " ";
message << converted[i]->AsString();
+ }
- AdblockPlus::ErrorCallbackPtr callback = jsEngine->GetErrorCallback();
- (*callback)(message.str());
+ std::stringstream source;
+ v8::Local<v8::StackFrame> frame = v8::StackTrace::CurrentStackTrace(1)->GetFrame(0);
+ source << AdblockPlus::Utils::FromV8String(frame->GetScriptName());
+ source << ":" << frame->GetLineNumber();
+
+ AdblockPlus::LogSystemPtr callback = jsEngine->GetLogSystem();
+ (*callback)(logLevel, message.str(), source.str());
return v8::Undefined();
}
+ v8::Handle<v8::Value> LogCallback(const v8::Arguments& arguments)
+ {
+ return DoLog(AdblockPlus::LogSystem::LOG, arguments);
+ }
+
+ v8::Handle<v8::Value> DebugCallback(const v8::Arguments& arguments)
+ {
+ return DoLog(AdblockPlus::LogSystem::LOG, arguments);
+ }
+
+ v8::Handle<v8::Value> InfoCallback(const v8::Arguments& arguments)
+ {
+ return DoLog(AdblockPlus::LogSystem::INFO, arguments);
+ }
+
+ v8::Handle<v8::Value> WarnCallback(const v8::Arguments& arguments)
+ {
+ return DoLog(AdblockPlus::LogSystem::WARN, arguments);
+ }
+
+ v8::Handle<v8::Value> ErrorCallback(const v8::Arguments& arguments)
+ {
+ return DoLog(AdblockPlus::LogSystem::ERROR, arguments);
+ }
+
v8::Handle<v8::Value> TraceCallback(const v8::Arguments& arguments)
{
+ AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arguments);
+ const AdblockPlus::JsEngine::Context context(jsEngine);
+ AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
+
+ std::stringstream traceback;
+ v8::Local<v8::StackTrace> frames = v8::StackTrace::CurrentStackTrace(100);
+ for (int i = 0, l = frames->GetFrameCount(); i < l; i++)
+ {
+ v8::Local<v8::StackFrame> frame = frames->GetFrame(i);
+ traceback << (i + 1) << ": ";
+ std::string name = AdblockPlus::Utils::FromV8String(frame->GetFunctionName());
+ if (name.size())
+ traceback << name;
+ else
+ traceback << "/* anonymous */";
+ traceback << "() at ";
+ traceback << AdblockPlus::Utils::FromV8String(frame->GetScriptName());
+ traceback << ":" << frame->GetLineNumber();
+ traceback << std::endl;
+ }
+
+ AdblockPlus::LogSystemPtr callback = jsEngine->GetLogSystem();
+ (*callback)(AdblockPlus::LogSystem::TRACE, traceback.str(), "");
return v8::Undefined();
}
}
AdblockPlus::JsValuePtr AdblockPlus::ConsoleJsObject::Setup(
AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValuePtr obj)
{
+ obj->SetProperty("log", jsEngine->NewCallback(::LogCallback));
+ obj->SetProperty("debug", jsEngine->NewCallback(::DebugCallback));
+ obj->SetProperty("info", jsEngine->NewCallback(::InfoCallback));
+ obj->SetProperty("warn", jsEngine->NewCallback(::WarnCallback));
obj->SetProperty("error", jsEngine->NewCallback(::ErrorCallback));
obj->SetProperty("trace", jsEngine->NewCallback(::TraceCallback));
return obj;
}

Powered by Google App Engine
This is Rietveld