11from datetime import datetime , timedelta
22from typing import List , Dict , Optional , Any
33from bson import ObjectId
4+ import logging
5+
6+ logger = logging .getLogger (__name__ )
47
58
69class VoipCall :
@@ -57,6 +60,7 @@ def create_call(self, room_id: str, room_type: str, created_by: str, room_name:
5760 result = self .collection .insert_one (call_doc )
5861 call_doc ['id' ] = str (result .inserted_id )
5962 call_doc ['_id' ] = str (result .inserted_id )
63+ logger .info (f"New VOIP call created: { call_doc ['id' ]} in room { room_id } by { created_by } " )
6064 return self ._process_call (call_doc )
6165
6266 def get_active_call (self , room_id : str ) -> Optional [Dict [str , Any ]]:
@@ -124,9 +128,10 @@ def join_call(self, call_id: str, user_id: str) -> Optional[Dict[str, Any]]:
124128 )
125129
126130 if result .modified_count > 0 :
131+ logger .info (f"User { user_id } joined VOIP call { call_id } " )
127132 return self .get_call_by_id (call_id )
128133 except Exception as e :
129- print (f"Error joining call: { e } " )
134+ logger . error (f"Error joining call { call_id } for user { user_id } : { e } " )
130135 return None
131136
132137 def leave_call (self , call_id : str , user_id : str ) -> Optional [Dict [str , Any ]]:
@@ -146,11 +151,12 @@ def leave_call(self, call_id: str, user_id: str) -> Optional[Dict[str, Any]]:
146151 call = self .get_call_by_id (call_id )
147152 if call and len (call .get ('participants' , [])) == 0 :
148153 # End the call
149- self .end_call (call_id )
154+ logger .info (f"Ending call { call_id } because no participants remain after user { user_id } left" )
155+ self .end_call (call_id , 'no_participants' )
150156 return {'ended' : True , 'call_id' : call_id }
151157 return call
152158 except Exception as e :
153- print (f"Error leaving call: { e } " )
159+ logger . error (f"Error leaving call { call_id } for user { user_id } : { e } " )
154160 return None
155161
156162 def end_call (self , call_id : str , reason : str = None ) -> bool :
@@ -166,9 +172,11 @@ def end_call(self, call_id: str, reason: str = None) -> bool:
166172 }
167173 }
168174 )
175+ if result .modified_count > 0 :
176+ logger .info (f"Call { call_id } ended. Reason: { reason or 'Not specified' } " )
169177 return result .modified_count > 0
170178 except Exception as e :
171- print (f"Error ending call: { e } " )
179+ logger . error (f"Error ending call { call_id } : { e } " )
172180 return False
173181
174182 def set_mute_status (self , call_id : str , user_id : str , is_muted : bool ) -> bool :
@@ -282,7 +290,8 @@ def check_and_remove_offline_participants(self) -> List[Dict[str, Any]]:
282290 # Check if call is now empty
283291 updated_call = self .get_call_by_id (call_id )
284292 if updated_call and len (updated_call .get ('participants' , [])) == 0 :
285- self .end_call (call_id , 'no_participants' )
293+ logger .info (f"Ending call { call_id } during offline cleanup (no participants remain)" )
294+ self .end_call (call_id , 'no_participants_offline' )
286295 affected .append ({
287296 'call_id' : call_id ,
288297 'removed_users' : removed_users ,
0 commit comments