Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
B
B.I.T.S. Server
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
Operations
Operations
Incidents
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Projects
B.I.T.S. 4.0
B.I.T.S. Server
Commits
c418ef91
Commit
c418ef91
authored
Oct 22, 2014
by
Stefano Sanfilippo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Lazy construction for log messages.
parent
953a7720
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
31 additions
and
32 deletions
+31
-32
bitsd/common.py
bitsd/common.py
+3
-3
bitsd/listener/handlers.py
bitsd/listener/handlers.py
+6
-6
bitsd/listener/hooks.py
bitsd/listener/hooks.py
+9
-9
bitsd/persistence/engine.py
bitsd/persistence/engine.py
+3
-3
bitsd/server/auth.py
bitsd/server/auth.py
+3
-3
bitsd/server/handlers.py
bitsd/server/handlers.py
+5
-6
bitsd/server/notifier.py
bitsd/server/notifier.py
+2
-2
No files found.
bitsd/common.py
View file @
c418ef91
...
...
@@ -32,17 +32,17 @@ def bind(server, port, usocket, address=None):
# If we have a unix socket path
if
usocket
:
LOG
.
info
(
'Starting on unix socket
`{}`'
.
format
(
usocket
)
)
LOG
.
info
(
'Starting on unix socket
%r'
,
usocket
)
try
:
socket
=
bind_unix_socket
(
usocket
,
mode
=
options
.
usocket_mode
)
os
.
chown
(
usocket
,
options
.
usocket_uid
,
options
.
usocket_gid
)
except
OSError
as
error
:
LOG
.
error
(
'Cannot create unix socket:
{}'
.
format
(
error
)
)
LOG
.
error
(
'Cannot create unix socket:
%r'
,
error
)
else
:
server
.
add_socket
(
socket
)
LOG
.
info
(
'Started'
)
else
:
LOG
.
info
(
'Starting on port
{}'
.
format
(
port
)
)
LOG
.
info
(
'Starting on port
%d'
,
port
)
sockets
=
bind_sockets
(
port
,
address
=
address
)
server
.
add_sockets
(
sockets
)
LOG
.
info
(
'Started'
)
bitsd/listener/handlers.py
View file @
c418ef91
...
...
@@ -23,12 +23,12 @@ from .hooks import *
def
send
(
string
):
if
RemoteListener
.
STREAM
is
None
:
LOG
.
error
(
"No Fonera connected! Not sending
{!r}"
.
format
(
string
)
)
LOG
.
error
(
"No Fonera connected! Not sending
%r"
,
string
)
return
try
:
RemoteListener
.
STREAM
.
write
(
string
)
except
StreamClosedError
as
error
:
LOG
.
error
(
'Could not push message to Fonera!
{}'
.
format
(
error
)
)
LOG
.
error
(
'Could not push message to Fonera!
%s'
,
error
)
class
RemoteListener
(
tornado
.
tcpserver
.
TCPServer
):
...
...
@@ -89,10 +89,10 @@ class RemoteListener(tornado.tcpserver.TCPServer):
LOG
.
info
(
"New connection from Fonera."
)
if
address
[
0
]
!=
options
.
control_remote_address
:
LOG
.
error
(
"Connection from `
{}`, expected from `{}`. Ignoring."
.
format
(
"Connection from `
%s`, expected from `%s`. Ignoring."
,
address
,
options
.
control_remote_address
)
)
)
return
if
RemoteListener
.
STREAM
is
not
None
:
LOG
.
warning
(
"Another connection was open, closing the previous one."
)
...
...
@@ -115,14 +115,14 @@ class RemoteListener(tornado.tcpserver.TCPServer):
try
:
handler
=
RemoteListener
.
ACTIONS
[
action
]
except
KeyError
:
LOG
.
warning
(
'Remote received unknown command
{}'
.
format
(
args
)
)
LOG
.
warning
(
'Remote received unknown command
`%s`'
,
args
)
else
:
# Execute handler (index 0) with args (index 1->end)
try
:
handler
(
*
args
[
1
:])
except
TypeError
:
LOG
.
error
(
'Command
{} called with wrong number of args'
.
format
(
action
)
'Command
`%s` called with wrong number of args'
,
action
)
else
:
LOG
.
warning
(
'Remote received empty command.'
)
bitsd/listener/hooks.py
View file @
c418ef91
...
...
@@ -38,7 +38,7 @@ __all__ = [
def
handle_temperature_command
(
sensorid
,
value
):
"""Receives and log data received from remote sensor."""
LOG
.
info
(
'Received temperature: sensorid=
{}, value={}'
.
format
(
sensorid
,
value
)
)
LOG
.
info
(
'Received temperature: sensorid=
%r, value=%r'
,
sensorid
,
value
)
try
:
sensorid
=
int
(
sensorid
)
value
=
float
(
value
)
...
...
@@ -55,14 +55,14 @@ def handle_status_command(status):
"""Update status.
Will reject two identical and consecutive updates
(prevents opening when already open and vice-versa)."""
LOG
.
info
(
'Received status:
{}'
.
format
(
status
)
)
LOG
.
info
(
'Received status:
%r'
,
status
)
try
:
status
=
int
(
status
)
except
ValueError
:
LOG
.
error
(
'Wrong type for parameters in temperature command'
)
return
if
status
not
in
(
0
,
1
):
LOG
.
error
(
'Non existent status
{}, ignoring.'
.
format
(
status
)
)
LOG
.
error
(
'Non existent status
%r, ignoring.'
,
status
)
return
textstatus
=
Status
.
OPEN
if
status
==
1
else
Status
.
CLOSED
...
...
@@ -78,7 +78,7 @@ def handle_status_command(status):
def
handle_enter_command
(
userid
):
"""Handles signal triggered when a new user enters."""
LOG
.
info
(
'Received enter command: id=
{}'
.
format
(
userid
)
)
LOG
.
info
(
'Received enter command: id=
%r'
,
userid
)
try
:
userid
=
int
(
userid
)
except
ValueError
:
...
...
@@ -90,7 +90,7 @@ def handle_enter_command(userid):
def
handle_leave_command
(
userid
):
"""Handles signal triggered when a known user leaves."""
LOG
.
info
(
'Received leave command: id=
{}'
.
format
(
userid
)
)
LOG
.
info
(
'Received leave command: id=
%r'
,
userid
)
try
:
userid
=
int
(
userid
)
except
ValueError
:
...
...
@@ -102,11 +102,11 @@ def handle_leave_command(userid):
def
handle_message_command
(
message
):
"""Handles message broadcast requests."""
LOG
.
info
(
'Received message command: message=
{!r}'
.
format
(
message
)
)
LOG
.
info
(
'Received message command: message=
%r'
,
message
)
try
:
decodedmex
=
base64
.
b64decode
(
message
)
except
TypeError
:
LOG
.
error
(
'Received message is not valid base64:
{!r}'
.
format
(
message
)
)
LOG
.
error
(
'Received message is not valid base64:
%r'
,
message
)
else
:
text
=
decodedmex
.
decode
(
'utf8'
)
#FIXME maybe get author ID from message?
...
...
@@ -114,7 +114,7 @@ def handle_message_command(message):
with
session_scope
()
as
session
:
user
=
query
.
get_user
(
session
,
user
)
if
not
user
:
LOG
.
error
(
"Non-existent user
{}, not logging message."
.
format
(
user
)
)
LOG
.
error
(
"Non-existent user
%r, not logging message."
,
user
)
return
message
=
query
.
log_message
(
session
,
user
,
text
)
broadcast
(
message
.
jsondict
())
...
...
@@ -123,7 +123,7 @@ def handle_message_command(message):
def
handle_sound_command
(
soundid
):
"""Handles requests to play a sound."""
LOG
.
info
(
'Received sound command: id=
{}'
.
format
(
soundid
)
)
LOG
.
info
(
'Received sound command: id=
%r'
,
soundid
)
try
:
soundid
=
int
(
soundid
)
except
ValueError
:
...
...
bitsd/persistence/engine.py
View file @
c418ef91
...
...
@@ -46,7 +46,7 @@ def session_scope():
yield
session
session
.
commit
()
except
IntegrityError
as
e
:
LOG
.
error
(
"Integrity error in DB, rolling back:
{}"
.
format
(
e
)
)
LOG
.
error
(
"Integrity error in DB, rolling back:
%s"
,
e
)
session
.
rollback
()
except
:
LOG
.
error
(
"Error in DB, rolling back."
)
...
...
@@ -62,7 +62,7 @@ def persist(session, data, flush=True):
**Note:** might log what's being persisted, so don't put clear text password
into `__str__()`."""
LOG
.
debug
(
'Persisting data
{}'
.
format
(
data
)
)
LOG
.
debug
(
'Persisting data
%r'
,
data
)
session
.
add
(
data
)
if
flush
:
session
.
flush
()
...
...
@@ -71,7 +71,7 @@ def persist(session, data, flush=True):
def
delete
(
session
,
data
):
"""Delete data from DB."""
LOG
.
debug
(
'Deleting
{}'
.
format
(
data
)
)
LOG
.
debug
(
'Deleting
%r'
,
data
)
session
.
delete
(
data
)
...
...
bitsd/server/auth.py
View file @
c418ef91
...
...
@@ -87,15 +87,15 @@ def verify(session, username, supplied_password, ip_address, has_captcha, recapt
last_attempt_for_ip_and_username
=
get_last_login_attempt
(
session
,
ip_address
,
username
)
if
detect_dos
(
last_attempt_for_ip
,
timedelta
(
seconds
=
1
)):
raise
DoSError
(
"Too frequent requests from
{}"
.
format
(
ip_address
)
)
raise
DoSError
(
"Too frequent requests from
%s"
,
ip_address
)
if
detect_dos
(
last_attempt_for_ip_and_username
,
timedelta
(
seconds
=
options
.
min_login_retry
)):
raise
DoSError
(
"Too frequent attempts from
{} for username `{}`"
.
format
(
ip_address
,
username
)
)
raise
DoSError
(
"Too frequent attempts from
%s for username %r"
,
ip_address
,
username
)
user
=
get_user
(
session
,
username
)
if
user
is
None
:
LOG
.
warn
(
"Failed attempt for non existent user
`{}`"
.
format
(
username
)
)
LOG
.
warn
(
"Failed attempt for non existent user
%r"
,
username
)
# Calculate hash anyway (see docs for the explanation)
Hasher
.
encrypt
(
supplied_password
)
log_last_login_attempt
(
session
,
ip_address
,
username
)
...
...
bitsd/server/handlers.py
View file @
c418ef91
...
...
@@ -223,7 +223,7 @@ class LoginPageHandler(BaseHandler):
try
:
verified
=
verify
(
session
,
username
,
password
,
ip_address
,
has_recaptcha
,
captcha_challenge
,
captcha_response
)
except
DoSError
as
error
:
LOG
.
warning
(
"DoS protection:
{}"
.
format
(
error
)
)
LOG
.
warning
(
"DoS protection:
%s"
,
error
)
self
.
log_offender_details
()
self
.
render
(
'templates/login.html'
,
...
...
@@ -240,10 +240,10 @@ class LoginPageHandler(BaseHandler):
username
,
expires_days
=
options
.
cookie_max_age_days
)
LOG
.
info
(
"Authenticating user
`{}`"
.
format
(
username
)
)
LOG
.
info
(
"Authenticating user
%r"
,
username
)
self
.
redirect
(
next
)
else
:
LOG
.
warning
(
"Failed authentication for user
`{}`"
.
format
(
username
)
)
LOG
.
warning
(
"Failed authentication for user
%r"
,
username
)
self
.
log_offender_details
()
self
.
render
(
'templates/login.html'
,
...
...
@@ -295,8 +295,7 @@ class AdminPageHandler(BaseHandler):
else
:
textstatus
=
Status
.
OPEN
if
curstatus
.
value
==
Status
.
CLOSED
else
Status
.
CLOSED
LOG
.
info
(
'Change of BITS to status={}'
.
format
(
textstatus
)
+
' from web interface.'
)
LOG
.
info
(
'Change of BITS to status=%r from web interface.'
,
textstatus
)
message
=
''
try
:
status
=
query
.
log_status
(
session
,
textstatus
,
'web'
)
...
...
@@ -334,7 +333,7 @@ class MessagePageHandler(BaseHandler):
text
=
self
.
get_argument
(
'msgtext'
)
username
=
self
.
get_current_user
()
LOG
.
info
(
"
{} sent message {!r} from web"
.
format
(
username
,
text
)
)
LOG
.
info
(
"
%r sent message %r from web"
,
username
,
text
)
with
session_scope
()
as
session
:
user
=
query
.
get_user
(
session
,
username
)
...
...
bitsd/server/notifier.py
View file @
c418ef91
...
...
@@ -20,12 +20,12 @@ class MessageNotifier(object):
def
register
(
self
,
client
):
"""Add a new handler to the clients list."""
LOG
.
debug
(
'Adding client
{} to {}'
.
format
(
client
,
self
.
name
)
)
LOG
.
debug
(
'Adding client
%r to %r'
,
client
,
self
.
name
)
self
.
clients
.
append
(
client
)
def
unregister
(
self
,
client
):
"""Remove the handler from the clients list."""
LOG
.
debug
(
'Removing client
{} from {}'
.
format
(
client
,
self
.
name
)
)
LOG
.
debug
(
'Removing client
%r from %r'
,
client
,
self
.
name
)
self
.
clients
.
remove
(
client
)
def
broadcast
(
self
,
message
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment