Skip to content

Commit 896b0df

Browse files
committed
Use CEF logging functions (cztomczak#352) and others (cztomczak#351, cztomczak#277)...
Fix logging command line string for sub-processes (cztomczak#351). Remove CefExecuteProcess call in cef.Initialize. This call should happen only in subprocess main.cpp.
1 parent b359129 commit 896b0df

38 files changed

+293
-489
lines changed

api/ApplicationSettings.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,9 @@ a value of "verbose", "info", "warning", "error", "error-report" or
287287

288288
Accepted values - constants available in the cefpython module:
289289
* LOGSEVERITY_VERBOSE
290-
* LOGSEVERITY_INFO (default)
290+
* LOGSEVERITY_INFO
291291
* LOGSEVERITY_WARNING
292-
* LOGSEVERITY_ERROR
292+
* LOGSEVERITY_ERROR (default)
293293
* LOGSEVERITY_ERROR_REPORT
294294
* LOGSEVERITY_DISABLE
295295

docs/Knowledge-Base.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,12 @@ You can pass "--debug" command line flag to any of CEF Python
167167
examples and unit tests. It will also work with your app, as
168168
this feature is enabled in CEF Python's core. When this flag is
169169
passed the following settings will be set:
170-
```
171-
settings = {"debug": True, "log_severity": cef.LOGSEVERITY_WARNING}
170+
```python
171+
settings = {
172+
"debug": True,
173+
"log_severity": cef.LOGSEVERITY_INFO,
174+
"log_file": "debug.log",
175+
}
172176
cef.Initialize(settings=settings)
173177
```
174178

docs/Tutorial.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ To enable debugging set these settings:
244244
```python
245245
settings = {
246246
"debug": True,
247-
"log_severity": cef.LOGSEVERITY_WARNING,
247+
"log_severity": cef.LOGSEVERITY_INFO,
248248
"log_file": "debug.log",
249249
}
250250
cef.Initialize(settings=settings)

examples/wxpython.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# Example of embedding CEF Python browser using wxPython library.
22
# This example has a top menu and a browser widget without navigation bar.
33

4-
# To install wxPython on Linux type "sudo apt-get install python-wxtools".
5-
64
# Tested configurations:
5+
# - wxPython 4.0 on Windows/Linux
76
# - wxPython 3.0 on Windows/Mac
87
# - wxPython 2.8 on Linux
98
# - CEF Python v55.4+
@@ -35,8 +34,7 @@ def main():
3534
sys.excepthook = cef.ExceptHook # To shutdown all CEF processes on error
3635
settings = {}
3736
if WINDOWS:
38-
# High DPI support
39-
settings["auto_zooming"] = "system_dpi"
37+
settings["auto_zooming"] = "system_dpi" # High DPI support
4038
# noinspection PyUnresolvedReferences, PyArgumentList
4139
cef.DpiAware.SetProcessDpiAware() # Alternative is to embed manifest
4240
cef.Initialize(settings=settings)

src/cefpython.pyx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -438,12 +438,12 @@ from cef_image cimport *
438438
from main_message_loop cimport *
439439
# noinspection PyUnresolvedReferences
440440
from cef_views cimport *
441+
from cef_log cimport *
441442

442443
# -----------------------------------------------------------------------------
443444
# GLOBAL VARIABLES
444445

445446
g_debug = False
446-
g_debugFile = "debug.log"
447447

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

463463
cdef py_bool g_MessageLoop_called = False
464464
cdef py_bool g_MessageLoopWork_called = False
465+
cdef py_bool g_cef_initialized = False
465466

466467
cdef dict g_globalClientCallbacks = {}
467468

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

532533
cdef public void cefpython_GetDebugOptions(
533-
cpp_bool* debug,
534-
cpp_string* debugFile
534+
cpp_bool* debug
535535
) except * with gil:
536536
# Called from subprocess/cefpython_app.cpp -> CefPythonApp constructor.
537-
cdef cpp_string cppString = PyStringToChar(g_debugFile)
538537
try:
539538
debug[0] = <cpp_bool>bool(g_debug)
540-
debugFile.assign(cppString)
541539
except:
542540
(exc_type, exc_value, exc_trace) = sys.exc_info()
543541
sys.excepthook(exc_type, exc_value, exc_trace)
@@ -618,16 +616,17 @@ def Initialize(applicationSettings=None, commandLineSwitches=None, **kwargs):
618616
# Debug settings need to be set before Debug() is called
619617
# and before the CefPythonApp class is instantiated.
620618
global g_debug
621-
global g_debugFile
622619
if "--debug" in sys.argv:
623620
application_settings["debug"] = True
624-
application_settings["log_file"] = "debug.log"
625-
application_settings["log_severity"] = LOGSEVERITY_WARNING
621+
application_settings["log_file"] = os.path.join(os.getcwd(),
622+
"debug.log")
623+
application_settings["log_severity"] = LOGSEVERITY_INFO
626624
sys.argv.remove("--debug")
627625
if "debug" in application_settings:
628626
g_debug = bool(application_settings["debug"])
629-
if "log_file" in application_settings:
630-
g_debugFile = application_settings["log_file"]
627+
if "log_severity" in application_settings:
628+
if application_settings["log_severity"] <= LOGSEVERITY_INFO:
629+
g_debug = True
631630

632631
Debug("Initialize() called")
633632

@@ -719,11 +718,9 @@ def Initialize(applicationSettings=None, commandLineSwitches=None, **kwargs):
719718
# TODO: use the CefMainArgs(int argc, char** argv) constructor.
720719
cdef CefMainArgs cefMainArgs
721720
cdef int exitCode = 1
722-
with nogil:
723-
exitCode = CefExecuteProcess(cefMainArgs, cefApp, NULL)
724-
Debug("CefExecuteProcess(): exitCode = %s" % exitCode)
725-
if exitCode >= 0:
726-
sys.exit(exitCode)
721+
722+
# NOTE: CefExecuteProcess shall not be called here. It should
723+
# be called only in the subprocess main.cpp.
727724

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

755+
global g_cef_initialized
756+
g_cef_initialized = True
757+
758758
if not ret:
759759
Debug("CefInitialize() failed")
760760

src/client_handler/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ SRC = client_handler.cpp cookie_visitor.cpp resource_handler.cpp \
2323
download_handler.cpp focus_handler.cpp js_dialog_handler.cpp \
2424
keyboard_handler.cpp lifespan_handler.cpp load_handler.cpp \
2525
render_handler.cpp request_handler.cpp dialog_handler.cpp \
26+
cef_log.cpp \
2627
$(SRC_MORE)
2728

2829
OBJ = $(filter %.o, $(SRC:.cpp=.o) $(SRC:.mm=.o))

src/client_handler/cef_log.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2017 CEF Python, see the Authors file.
2+
// All rights reserved. Licensed under BSD 3-clause license.
3+
// Project website: https://github.com/cztomczak/cefpython
4+
5+
#include "include/base/cef_logging.h"
6+
7+
void cef_log_info(char* msg) {
8+
LOG(INFO) << msg;
9+
}
10+
11+
void cef_log_warning(char* msg) {
12+
LOG(WARNING) << msg;
13+
}
14+
15+
void cef_log_error(char* msg) {
16+
LOG(ERROR) << msg;
17+
}

src/client_handler/cef_log.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) 2017 CEF Python, see the Authors file.
2+
// All rights reserved. Licensed under BSD 3-clause license.
3+
// Project website: https://github.com/cztomczak/cefpython
4+
5+
#pragma once
6+
7+
void cef_log_info(char* msg);
8+
void cef_log_warning(char* msg);
9+
void cef_log_error(char* msg);

src/client_handler/client_handler.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "client_handler.h"
1010
#include "common/cefpython_public_api.h"
11-
#include "common/DebugLog.h"
11+
#include "include/base/cef_logging.h"
1212

1313
#if defined(OS_WIN)
1414
#include <Shellapi.h>
@@ -34,9 +34,9 @@ bool ClientHandler::OnProcessMessageReceived(
3434
return false;
3535
}
3636
std::string messageName = message->GetName().ToString();
37-
std::string logMessage = "Browser: OnProcessMessageReceived(): ";
37+
std::string logMessage = "[Browser process] OnProcessMessageReceived(): ";
3838
logMessage.append(messageName.c_str());
39-
DebugLog(logMessage.c_str());
39+
LOG(INFO) << logMessage.c_str();
4040
if (messageName == "OnContextCreated") {
4141
CefRefPtr<CefListValue> arguments = message->GetArgumentList();
4242
if (arguments->GetSize() == 1 && arguments->GetType(0) == VTYPE_INT) {
@@ -45,8 +45,8 @@ bool ClientHandler::OnProcessMessageReceived(
4545
V8ContextHandler_OnContextCreated(browser, frame);
4646
return true;
4747
} else {
48-
DebugLog("Browser: OnProcessMessageReceived(): invalid arguments" \
49-
", messageName = OnContextCreated");
48+
LOG(ERROR) << "[Browser process] OnProcessMessageReceived():"
49+
" invalid arguments, messageName=OnContextCreated";
5050
return false;
5151
}
5252
} else if (messageName == "OnContextReleased") {
@@ -59,8 +59,8 @@ bool ClientHandler::OnProcessMessageReceived(
5959
V8ContextHandler_OnContextReleased(browserId, frameId);
6060
return true;
6161
} else {
62-
DebugLog("Browser: OnProcessMessageReceived(): invalid arguments" \
63-
", messageName = OnContextReleased");
62+
LOG(ERROR) << "[Browser process] OnProcessMessageReceived():"
63+
" invalid arguments, messageName=OnContextReleased";
6464
return false;
6565
}
6666
} else if (messageName == "V8FunctionHandler::Execute") {
@@ -80,8 +80,9 @@ bool ClientHandler::OnProcessMessageReceived(
8080
functionArguments);
8181
return true;
8282
} else {
83-
DebugLog("Browser: OnProcessMessageReceived(): invalid arguments" \
84-
", messageName = V8FunctionHandler::Execute");
83+
LOG(ERROR) << "[Browser process] OnProcessMessageReceived():"
84+
" invalid arguments,"
85+
" messageName=V8FunctionHandler::Execute";
8586
return false;
8687
}
8788
} else if (messageName == "ExecutePythonCallback") {
@@ -94,8 +95,9 @@ bool ClientHandler::OnProcessMessageReceived(
9495
ExecutePythonCallback(browser, callbackId, functionArguments);
9596
return true;
9697
} else {
97-
DebugLog("Browser: OnProcessMessageReceived(): invalid arguments" \
98-
", messageName = ExecutePythonCallback");
98+
LOG(ERROR) << "[Browser process] OnProcessMessageReceived():"
99+
" invalid arguments,"
100+
" messageName=ExecutePythonCallback";
99101
return false;
100102
}
101103
} else if (messageName == "RemovePythonCallbacksForFrame") {
@@ -105,8 +107,9 @@ bool ClientHandler::OnProcessMessageReceived(
105107
RemovePythonCallbacksForFrame(frameId);
106108
return true;
107109
} else {
108-
DebugLog("Browser: OnProcessMessageReceived(): invalid arguments" \
109-
", messageName = ExecutePythonCallback");
110+
LOG(ERROR) << "[Browser process] OnProcessMessageReceived():"
111+
" invalid arguments,"
112+
" messageName=ExecutePythonCallback";
110113
return false;
111114
}
112115
}

src/client_handler/context_menu_handler.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Project website: https://github.com/cztomczak/cefpython
44

55
#include "context_menu_handler.h"
6-
#include "common/DebugLog.h"
6+
#include "include/base/cef_logging.h"
77

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

129129
if (url.empty()) {
130-
DebugLog("Browser: OpenInExternalBrowser() FAILED: url is empty");
130+
LOG(ERROR) << "[Browser process] OpenInExternalBrowser():"
131+
" url is empty";
131132
return;
132133
}
133-
std::string msg = "Browser: OpenInExternalBrowser(): url=";
134+
std::string msg = "[Browser process] OpenInExternalBrowser(): url=";
134135
msg.append(url.c_str());
135-
DebugLog(msg.c_str());
136+
LOG(INFO) << msg.c_str();
136137

137138
// xdg-open is a desktop-independent tool for running
138139
// default applications. Installed by default on Ubuntu.

0 commit comments

Comments
 (0)