3737_Debug = True
3838_DebugLevel = 10
3939
40+ _APILogFileEnabled = False
41+
4042#------------------------------------------------------------------------------
4143
4244import os
8183
8284def init ():
8385 global _AllAPIMethods
86+ global _APILogFileEnabled
87+ _APILogFileEnabled = settings .config .conf ().getBool ('logs/api-enabled' )
8488 from bitdust .interface import api
8589 encrypted_web_socket .SetIncomingAPIMessageCallback (do_process_incoming_message )
8690 routed_web_socket .SetIncomingAPIMessageCallback (do_process_incoming_message )
@@ -96,6 +100,7 @@ def init():
96100 'RESULT' ,
97101 '_Debug' ,
98102 '_DebugLevel' ,
103+ '_APILogFileEnabled' ,
99104 'strng' ,
100105 'sys' ,
101106 'time' ,
@@ -428,6 +433,7 @@ def reset_authorization(device_name):
428433
429434def do_process_incoming_message (device_object , json_data ):
430435 global _AllAPIMethods
436+ global _APILogFileEnabled
431437 from bitdust .interface import api
432438 command = json_data .get ('command' )
433439 if command == 'api_call' :
@@ -460,8 +466,8 @@ def do_process_incoming_message(device_object, json_data):
460466 if _Debug :
461467 lg .out (_DebugLevel , '*** %s API WS IN %s(%r)' % (call_id , method , kwargs ))
462468
463- # if _APILogFileEnabled:
464- # lg.out(0, '*** %s WS IN %s(%r)' % (call_id, method, kwargs), log_name='api', showtime=True)
469+ if _APILogFileEnabled :
470+ lg .out (0 , '*** %s WS IN %s %s(%r)' % (device_object . device_name , call_id , method , kwargs ), log_name = 'api' , showtime = True )
465471
466472 func = getattr (api , method )
467473 try :
@@ -520,42 +526,38 @@ def _eb(err):
520526
521527
522528def on_event (evt ):
523- for inst in instances ().values ():
524- inst .on_outgoing_message ({
525- 'cmd' : 'push' ,
526- 'type' : 'event' ,
527- 'payload' : {
528- 'event_id' : evt .event_id ,
529- 'data' : evt .data ,
530- },
531- })
529+ push ({
530+ 'cmd' : 'push' ,
531+ 'type' : 'event' ,
532+ 'payload' : {
533+ 'event_id' : evt .event_id ,
534+ 'data' : evt .data ,
535+ },
536+ })
532537
533538
534539def on_stream_message (message_json ):
535- for inst in instances ().values ():
536- inst .on_outgoing_message ({
537- 'cmd' : 'push' ,
538- 'type' : 'stream_message' ,
539- 'payload' : message_json ,
540- })
540+ push ({
541+ 'cmd' : 'push' ,
542+ 'type' : 'stream_message' ,
543+ 'payload' : message_json ,
544+ })
541545
542546
543547def on_online_status_changed (status_info ):
544- for inst in instances ().values ():
545- inst .on_outgoing_message ({
546- 'cmd' : 'push' ,
547- 'type' : 'online_status' ,
548- 'payload' : status_info ,
549- })
548+ push ({
549+ 'cmd' : 'push' ,
550+ 'type' : 'online_status' ,
551+ 'payload' : status_info ,
552+ })
550553
551554
552555def on_model_changed (snapshot_object ):
553- for inst in instances ().values ():
554- inst .on_outgoing_message ({
555- 'cmd' : 'push' ,
556- 'type' : 'model' ,
557- 'payload' : snapshot_object .to_json (),
558- })
556+ push ({
557+ 'cmd' : 'push' ,
558+ 'type' : 'model' ,
559+ 'payload' : snapshot_object .to_json (),
560+ })
559561
560562
561563def on_device_client_code_input_received (device_name , client_code ):
@@ -566,6 +568,17 @@ def on_device_client_code_input_received(device_name, client_code):
566568 inst .on_client_code_input_received (client_code )
567569
568570
571+ #------------------------------------------------------------------------------
572+
573+
574+ def push (json_data ):
575+ global _APILogFileEnabled
576+ for inst in instances ().values ():
577+ inst .on_outgoing_message (json_data )
578+ if _APILogFileEnabled :
579+ lg .out (0 , '*** WS PUSH %s : %r' % (inst .device_name , json_data ), log_name = 'api' , showtime = True )
580+
581+
569582#------------------------------------------------------------------------------
570583
571584if __name__ == '__main__' :
0 commit comments