Skip to content

Commit

Permalink
Use CEF logging functions (#352) and others (#351, #277)...
Browse files Browse the repository at this point in the history
Fix logging command line string for sub-processes (#351).

Remove CefExecuteProcess call in cef.Initialize. This call should
happen only in subprocess main.cpp.
  • Loading branch information
cztomczak committed Apr 19, 2017
1 parent b359129 commit 896b0df
Show file tree
Hide file tree
Showing 38 changed files with 293 additions and 489 deletions.
4 changes: 2 additions & 2 deletions api/ApplicationSettings.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,9 @@ a value of "verbose", "info", "warning", "error", "error-report" or

Accepted values - constants available in the cefpython module:
* LOGSEVERITY_VERBOSE
* LOGSEVERITY_INFO (default)
* LOGSEVERITY_INFO
* LOGSEVERITY_WARNING
* LOGSEVERITY_ERROR
* LOGSEVERITY_ERROR (default)
* LOGSEVERITY_ERROR_REPORT
* LOGSEVERITY_DISABLE

Expand Down
8 changes: 6 additions & 2 deletions docs/Knowledge-Base.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,12 @@ You can pass "--debug" command line flag to any of CEF Python
examples and unit tests. It will also work with your app, as
this feature is enabled in CEF Python's core. When this flag is
passed the following settings will be set:
```
settings = {"debug": True, "log_severity": cef.LOGSEVERITY_WARNING}
```python
settings = {
"debug": True,
"log_severity": cef.LOGSEVERITY_INFO,
"log_file": "debug.log",
}
cef.Initialize(settings=settings)
```

Expand Down
2 changes: 1 addition & 1 deletion docs/Tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ To enable debugging set these settings:
```python
settings = {
"debug": True,
"log_severity": cef.LOGSEVERITY_WARNING,
"log_severity": cef.LOGSEVERITY_INFO,
"log_file": "debug.log",
}
cef.Initialize(settings=settings)
Expand Down
6 changes: 2 additions & 4 deletions examples/wxpython.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Example of embedding CEF Python browser using wxPython library.
# This example has a top menu and a browser widget without navigation bar.

# To install wxPython on Linux type "sudo apt-get install python-wxtools".

# Tested configurations:
# - wxPython 4.0 on Windows/Linux
# - wxPython 3.0 on Windows/Mac
# - wxPython 2.8 on Linux
# - CEF Python v55.4+
Expand Down Expand Up @@ -35,8 +34,7 @@ def main():
sys.excepthook = cef.ExceptHook # To shutdown all CEF processes on error
settings = {}
if WINDOWS:
# High DPI support
settings["auto_zooming"] = "system_dpi"
settings["auto_zooming"] = "system_dpi" # High DPI support
# noinspection PyUnresolvedReferences, PyArgumentList
cef.DpiAware.SetProcessDpiAware() # Alternative is to embed manifest
cef.Initialize(settings=settings)
Expand Down
30 changes: 15 additions & 15 deletions src/cefpython.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,12 @@ from cef_image cimport *
from main_message_loop cimport *
# noinspection PyUnresolvedReferences
from cef_views cimport *
from cef_log cimport *

# -----------------------------------------------------------------------------
# GLOBAL VARIABLES

g_debug = False
g_debugFile = "debug.log"

# When put None here and assigned a local dictionary in Initialize(), later
# while running app this global variable was garbage collected, see topic:
Expand All @@ -462,6 +462,7 @@ cdef scoped_ptr[MainMessageLoopExternalPump] g_external_message_pump

cdef py_bool g_MessageLoop_called = False
cdef py_bool g_MessageLoopWork_called = False
cdef py_bool g_cef_initialized = False

cdef dict g_globalClientCallbacks = {}

Expand Down Expand Up @@ -530,14 +531,11 @@ include "handlers/v8function_handler.pyx"
# Utility functions to provide settings to the C++ browser process code.

cdef public void cefpython_GetDebugOptions(
cpp_bool* debug,
cpp_string* debugFile
cpp_bool* debug
) except * with gil:
# Called from subprocess/cefpython_app.cpp -> CefPythonApp constructor.
cdef cpp_string cppString = PyStringToChar(g_debugFile)
try:
debug[0] = <cpp_bool>bool(g_debug)
debugFile.assign(cppString)
except:
(exc_type, exc_value, exc_trace) = sys.exc_info()
sys.excepthook(exc_type, exc_value, exc_trace)
Expand Down Expand Up @@ -618,16 +616,17 @@ def Initialize(applicationSettings=None, commandLineSwitches=None, **kwargs):
# Debug settings need to be set before Debug() is called
# and before the CefPythonApp class is instantiated.
global g_debug
global g_debugFile
if "--debug" in sys.argv:
application_settings["debug"] = True
application_settings["log_file"] = "debug.log"
application_settings["log_severity"] = LOGSEVERITY_WARNING
application_settings["log_file"] = os.path.join(os.getcwd(),
"debug.log")
application_settings["log_severity"] = LOGSEVERITY_INFO
sys.argv.remove("--debug")
if "debug" in application_settings:
g_debug = bool(application_settings["debug"])
if "log_file" in application_settings:
g_debugFile = application_settings["log_file"]
if "log_severity" in application_settings:
if application_settings["log_severity"] <= LOGSEVERITY_INFO:
g_debug = True

Debug("Initialize() called")

Expand Down Expand Up @@ -719,11 +718,9 @@ def Initialize(applicationSettings=None, commandLineSwitches=None, **kwargs):
# TODO: use the CefMainArgs(int argc, char** argv) constructor.
cdef CefMainArgs cefMainArgs
cdef int exitCode = 1
with nogil:
exitCode = CefExecuteProcess(cefMainArgs, cefApp, NULL)
Debug("CefExecuteProcess(): exitCode = %s" % exitCode)
if exitCode >= 0:
sys.exit(exitCode)

# NOTE: CefExecuteProcess shall not be called here. It should
# be called only in the subprocess main.cpp.

# Make a copy as applicationSettings is a reference only
# that might get destroyed later.
Expand Down Expand Up @@ -755,6 +752,9 @@ def Initialize(applicationSettings=None, commandLineSwitches=None, **kwargs):
with nogil:
ret = CefInitialize(cefMainArgs, cefApplicationSettings, cefApp, NULL)

global g_cef_initialized
g_cef_initialized = True

if not ret:
Debug("CefInitialize() failed")

Expand Down
1 change: 1 addition & 0 deletions src/client_handler/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ SRC = client_handler.cpp cookie_visitor.cpp resource_handler.cpp \
download_handler.cpp focus_handler.cpp js_dialog_handler.cpp \
keyboard_handler.cpp lifespan_handler.cpp load_handler.cpp \
render_handler.cpp request_handler.cpp dialog_handler.cpp \
cef_log.cpp \
$(SRC_MORE)

OBJ = $(filter %.o, $(SRC:.cpp=.o) $(SRC:.mm=.o))
Expand Down
17 changes: 17 additions & 0 deletions src/client_handler/cef_log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) 2017 CEF Python, see the Authors file.
// All rights reserved. Licensed under BSD 3-clause license.
// Project website: https://github.com/cztomczak/cefpython

#include "include/base/cef_logging.h"

void cef_log_info(char* msg) {
LOG(INFO) << msg;
}

void cef_log_warning(char* msg) {
LOG(WARNING) << msg;
}

void cef_log_error(char* msg) {
LOG(ERROR) << msg;
}
9 changes: 9 additions & 0 deletions src/client_handler/cef_log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) 2017 CEF Python, see the Authors file.
// All rights reserved. Licensed under BSD 3-clause license.
// Project website: https://github.com/cztomczak/cefpython

#pragma once

void cef_log_info(char* msg);
void cef_log_warning(char* msg);
void cef_log_error(char* msg);
29 changes: 16 additions & 13 deletions src/client_handler/client_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "client_handler.h"
#include "common/cefpython_public_api.h"
#include "common/DebugLog.h"
#include "include/base/cef_logging.h"

#if defined(OS_WIN)
#include <Shellapi.h>
Expand All @@ -34,9 +34,9 @@ bool ClientHandler::OnProcessMessageReceived(
return false;
}
std::string messageName = message->GetName().ToString();
std::string logMessage = "Browser: OnProcessMessageReceived(): ";
std::string logMessage = "[Browser process] OnProcessMessageReceived(): ";
logMessage.append(messageName.c_str());
DebugLog(logMessage.c_str());
LOG(INFO) << logMessage.c_str();
if (messageName == "OnContextCreated") {
CefRefPtr<CefListValue> arguments = message->GetArgumentList();
if (arguments->GetSize() == 1 && arguments->GetType(0) == VTYPE_INT) {
Expand All @@ -45,8 +45,8 @@ bool ClientHandler::OnProcessMessageReceived(
V8ContextHandler_OnContextCreated(browser, frame);
return true;
} else {
DebugLog("Browser: OnProcessMessageReceived(): invalid arguments" \
", messageName = OnContextCreated");
LOG(ERROR) << "[Browser process] OnProcessMessageReceived():"
" invalid arguments, messageName=OnContextCreated";
return false;
}
} else if (messageName == "OnContextReleased") {
Expand All @@ -59,8 +59,8 @@ bool ClientHandler::OnProcessMessageReceived(
V8ContextHandler_OnContextReleased(browserId, frameId);
return true;
} else {
DebugLog("Browser: OnProcessMessageReceived(): invalid arguments" \
", messageName = OnContextReleased");
LOG(ERROR) << "[Browser process] OnProcessMessageReceived():"
" invalid arguments, messageName=OnContextReleased";
return false;
}
} else if (messageName == "V8FunctionHandler::Execute") {
Expand All @@ -80,8 +80,9 @@ bool ClientHandler::OnProcessMessageReceived(
functionArguments);
return true;
} else {
DebugLog("Browser: OnProcessMessageReceived(): invalid arguments" \
", messageName = V8FunctionHandler::Execute");
LOG(ERROR) << "[Browser process] OnProcessMessageReceived():"
" invalid arguments,"
" messageName=V8FunctionHandler::Execute";
return false;
}
} else if (messageName == "ExecutePythonCallback") {
Expand All @@ -94,8 +95,9 @@ bool ClientHandler::OnProcessMessageReceived(
ExecutePythonCallback(browser, callbackId, functionArguments);
return true;
} else {
DebugLog("Browser: OnProcessMessageReceived(): invalid arguments" \
", messageName = ExecutePythonCallback");
LOG(ERROR) << "[Browser process] OnProcessMessageReceived():"
" invalid arguments,"
" messageName=ExecutePythonCallback";
return false;
}
} else if (messageName == "RemovePythonCallbacksForFrame") {
Expand All @@ -105,8 +107,9 @@ bool ClientHandler::OnProcessMessageReceived(
RemovePythonCallbacksForFrame(frameId);
return true;
} else {
DebugLog("Browser: OnProcessMessageReceived(): invalid arguments" \
", messageName = ExecutePythonCallback");
LOG(ERROR) << "[Browser process] OnProcessMessageReceived():"
" invalid arguments,"
" messageName=ExecutePythonCallback";
return false;
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/client_handler/context_menu_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Project website: https://github.com/cztomczak/cefpython

#include "context_menu_handler.h"
#include "common/DebugLog.h"
#include "include/base/cef_logging.h"

#define _MENU_ID_DEVTOOLS MENU_ID_USER_FIRST + 1
#define _MENU_ID_RELOAD_PAGE MENU_ID_USER_FIRST + 2
Expand Down Expand Up @@ -127,12 +127,13 @@ void OpenInExternalBrowser(const std::string& url)
// Linux equivalent of ShellExecute

if (url.empty()) {
DebugLog("Browser: OpenInExternalBrowser() FAILED: url is empty");
LOG(ERROR) << "[Browser process] OpenInExternalBrowser():"
" url is empty";
return;
}
std::string msg = "Browser: OpenInExternalBrowser(): url=";
std::string msg = "[Browser process] OpenInExternalBrowser(): url=";
msg.append(url.c_str());
DebugLog(msg.c_str());
LOG(INFO) << msg.c_str();

// xdg-open is a desktop-independent tool for running
// default applications. Installed by default on Ubuntu.
Expand Down
4 changes: 2 additions & 2 deletions src/client_handler/dialog_handler_gtk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "include/cef_parser.h"
#include "include/wrapper/cef_helpers.h"

#include "LOG_DEBUG.h"
#include "include/base/cef_logging.h"

#include "dialog_handler_gtk.h"
#include "x11.h"
Expand Down Expand Up @@ -159,7 +159,7 @@ GtkWindow* GetWindow(CefRefPtr<CefBrowser> browser) {
// internally, so GTK wasn't yet initialized and must do it
// now, so that display is available. Also must install X11
// error handlers to avoid 'BadWindow' errors.
LOG_DEBUG << "Initialize GTK";
LOG(INFO) << "[Browser process] Initialize GTK";
gtk_init(0, NULL);
InstallX11ErrorHandlers();
// Now the display is available
Expand Down
15 changes: 8 additions & 7 deletions src/client_handler/download_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Project website: https://github.com/cztomczak/cefpython

#include "download_handler.h"
#include "common/DebugLog.h"
#include "include/base/cef_logging.h"


void DownloadHandler::OnBeforeDownload(
Expand All @@ -15,12 +15,13 @@ void DownloadHandler::OnBeforeDownload(
REQUIRE_UI_THREAD();
bool downloads_enabled = ApplicationSettings_GetBool("downloads_enabled");
if (downloads_enabled) {
std::string msg = "Browser: About to download file: ";
std::string msg = "[Browser process] About to download file: ";
msg.append(suggested_name.ToString().c_str());
DebugLog(msg.c_str());
LOG(INFO) << msg.c_str();
callback->Continue(suggested_name, true);
} else {
DebugLog("Browser: Tried to download file, but downloads are disabled");
LOG(INFO) << "[Browser process] Tried to download file,"
" but downloads are disabled";
}
}

Expand All @@ -32,10 +33,10 @@ void DownloadHandler::OnDownloadUpdated(
{
REQUIRE_UI_THREAD();
if (download_item->IsComplete()) {
std::string msg = "Browser: Download completed, saved to: ";
std::string msg = "[Browser process] Download completed, saved to: ";
msg.append(download_item->GetFullPath().ToString().c_str());
DebugLog(msg.c_str());
LOG(INFO) << msg.c_str();
} else if (download_item->IsCanceled()) {
DebugLog("Browser: Download was cancelled");
LOG(INFO) << "[Browser process] Download was cancelled";
}
}
Loading

0 comments on commit 896b0df

Please sign in to comment.