Skip to content

Commit 1c70717

Browse files
Changed how SimulaQron does logging: we no longer use the logging from the NetQASM library (which results in logging to /dev/null and unclear how to adapt it) but simply write out own log files for the deamons to /tmp. This now allows us to properly debug the code and pinpoint where/when what happens
1 parent 9ed311d commit 1c70717

File tree

17 files changed

+123
-75
lines changed

17 files changed

+123
-75
lines changed

examples/nativeMode/corrRNG/aliceTest.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def main():
127127
# Check if we should run a server (if this node is listed in classicalNet)
128128
if myName in classicalNet.hostDict:
129129
# Create the local classical server
130-
logging.debug("LOCAL %s: Creating classical server.", myName)
130+
logging.debug("APP %s: Creating classical server.", myName)
131131
myNode = localNode(virtualNet.hostDict[myName], classicalNet)
132132
else:
133133
myNode = None
@@ -137,5 +137,9 @@ def main():
137137

138138

139139
##################################################################################################
140-
logging.basicConfig(format="%(asctime)s:%(levelname)s:%(message)s", level=logging.DEBUG)
140+
logging.basicConfig(
141+
format="%(asctime)s:%(levelname)s:%(name)s:%(filename)s:%(lineno)d:%(message)s",
142+
level=logging.DEBUG,
143+
force=True
144+
)
141145
main()

examples/nativeMode/corrRNG/bobTest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,9 @@ def main():
148148

149149

150150
##################################################################################################
151-
logging.basicConfig(format="%(asctime)s:%(levelname)s:%(message)s", level=logging.DEBUG)
151+
logging.basicConfig(
152+
format="%(asctime)s:%(levelname)s:%(name)s:%(filename)s:%(lineno)d:%(message)s",
153+
level=logging.DEBUG,
154+
force=True
155+
)
152156
main()

simulaqron/local/setup.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2828
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929

30+
import logging
3031
import time
3132
from typing import Callable
3233

33-
from netqasm.logging.glob import get_netqasm_logger
3434
from twisted.internet import error
3535
from twisted.internet.defer import DeferredList
3636
from twisted.internet.error import ReactorNotRunning
@@ -39,21 +39,21 @@
3939
from simulaqron.general.host_config import SocketsConfig
4040
from simulaqron.reactor import reactor
4141

42-
_logger = get_netqasm_logger("setup-local")
42+
_logger = logging.getLogger("setup-local")
4343

4444

4545
#####################################################################################################
4646
#
4747
# setup_local
4848
#
49-
# Sets up the local classical comms server (if applicable), and connects to the local virtual node
50-
# and other classical communication servers.
49+
# Sets up the local classical application level comms server (if applicable), and connects to the local
50+
# virtual node and other classical communication servers.
5151

5252

5353
def setup_local(myName: str, virtualNet: SocketsConfig, classicalNet: SocketsConfig,
5454
lNode: pb.Root, func: Callable, *args, **kwargs):
5555
"""
56-
Sets up a local classical communication server (if desired according to the configuration file),
56+
Sets up a local classical applicaiton level communication server (if desired according to the configuration file),
5757
a client connection to the local virtual node quantum backend and a client connections to all other
5858
classical communication servers
5959
@@ -81,22 +81,22 @@ def setup_local(myName: str, virtualNet: SocketsConfig, classicalNet: SocketsCon
8181
if myName in classicalNet.hostDict:
8282
try:
8383
nb = classicalNet.hostDict[myName]
84-
_logger.debug("LOCAL %s: Starting local classical communication server (%s: %s, %d).",
84+
_logger.debug("SETUP_LOCAL %s: Starting local classical communication server (%s: %s, %d).",
8585
myName, nb.name, nb.hostname, nb.port
8686
)
8787
nb.root = lNode
8888
nb.factory = pb.PBServerFactory(nb.root)
8989
reactor.listenTCP(nb.port, nb.factory)
9090
except Exception as e:
91-
_logger.error("LOCAL %s: Cannot start classical communication servers: %s", myName, e)
91+
_logger.error("SETUP_LOCAL %s: Cannot start classical communication servers: %s", myName, e)
9292
return
9393

9494
# Give the server some time to start up
9595
time.sleep(3)
9696

9797
# Connect to the local virtual node simulating the "local" qubits
9898
node = virtualNet.hostDict[myName]
99-
_logger.debug("LOCAL %s: Connecting to local virtual node (%s: %s, %d).", myName, node.name, node.hostname,
99+
_logger.debug("SETUP_LOCAL %s: Connecting to local virtual node (%s: %s, %d).", myName, node.name, node.hostname,
100100
node.port)
101101
factory = pb.PBClientFactory()
102102
reactor.connectTCP(node.hostname, node.port, factory)
@@ -107,7 +107,7 @@ def setup_local(myName: str, virtualNet: SocketsConfig, classicalNet: SocketsCon
107107
for node in classicalNet.hostDict:
108108
nb = classicalNet.hostDict[node]
109109
if nb.name != myName:
110-
_logger.debug("LOCAL %s: Making classical connection to %s (%s: %s, %d).", myName, nb.name, nb.name,
110+
_logger.debug("SETUP_LOCAL %s: Making classical connection to %s (%s: %s, %d).", myName, nb.name, nb.name,
111111
nb.hostname, nb.port)
112112
nb.factory = pb.PBClientFactory()
113113
reactor.connectTCP(nb.hostname, nb.port, nb.factory)
@@ -133,7 +133,7 @@ def setup_local(myName: str, virtualNet: SocketsConfig, classicalNet: SocketsCon
133133

134134
def init_register(resList: DeferredList, myName: str, virtualNet: SocketsConfig, classicalNet: SocketsConfig,
135135
lNode: pb.Root, func: Callable, *args, **kwargs):
136-
_logger.debug("LOCAL %s: All connections set up.", myName)
136+
_logger.debug("SETUP_LOCAL %s: All connections set up.", myName)
137137

138138
# Retrieve the connection to the local virtual node, if successful
139139
j = 0
@@ -142,7 +142,7 @@ def init_register(resList: DeferredList, myName: str, virtualNet: SocketsConfig,
142142
if lNode is not None:
143143
lNode.set_virtual_node(virtRoot)
144144
else:
145-
_logger.error("LOCAL %s: Connection to virtual server failed!", myName)
145+
_logger.error("SETUP_LOCAL %s: Connection to virtual server failed!", myName)
146146
reactor.stop()
147147

148148
# Retrieve connections to the classical nodes
@@ -152,9 +152,9 @@ def init_register(resList: DeferredList, myName: str, virtualNet: SocketsConfig,
152152
j = j + 1
153153
if resList[j][0]:
154154
nb.root = resList[j][1]
155-
_logger.debug("LOCAL %s: Connected node %s with %s", myName, nb.name, nb.root)
155+
_logger.debug("SETUP_LOCAL %s: Connected node %s with %s", myName, nb.name, nb.root)
156156
else:
157-
_logger.error("LOCAL %s: Connection to %s failed!", myName, nb.name)
157+
_logger.error("SETUP_LOCAL %s: Connection to %s failed!", myName, nb.name)
158158
reactor.stop()
159159

160160
# On the local virtual node, we still want to initialize a qubit register
@@ -164,7 +164,7 @@ def init_register(resList: DeferredList, myName: str, virtualNet: SocketsConfig,
164164

165165

166166
def fill_register(obj, myName, lNode, virtRoot, classicalNet, func, *args, **kwargs):
167-
_logger.debug("LOCAL %s: Created quantum register at virtual node.", myName)
167+
_logger.debug("SETUP_LOCAL %s: Created quantum register at virtual node.", myName)
168168
qReg = obj
169169

170170
# If we run a server, record the handle to the local virtual register
@@ -179,7 +179,7 @@ def localError(reason):
179179
"""
180180
Error handling for the connection.
181181
"""
182-
_logger.error("Critical error: %s", reason)
182+
_logger.error("SETUP_LOCAL: Critical error: %s", reason)
183183
try:
184184
reactor.stop()
185185
except ReactorNotRunning:

simulaqron/netqasm_backend/factory.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
from netqasm.backend.messages import MessageHeader, ErrorCode, deserialize_host_msg, Message, \
3232
InitNewAppMessage
33-
from netqasm.logging.glob import get_netqasm_logger
33+
import logging
3434
from twisted.internet.defer import DeferredLock, inlineCallbacks
3535
from twisted.internet.protocol import Factory, Protocol, connectionDone
3636
from twisted.internet.task import deferLater
@@ -81,7 +81,7 @@ def __init__(self, factory: "NetQASMFactory"):
8181
# Convenience
8282
self.name = self.factory.name
8383

84-
self._logger = get_netqasm_logger(f"{self.__class__.__name__}({self.name})")
84+
self._logger = logging.getLogger(f"{self.__class__.__name__}({self.name})")
8585
self._logger.debug("Initialized Protocol")
8686

8787
def connectionMade(self):
@@ -195,7 +195,7 @@ def __init__(
195195
# Lock governing access to the qubitList
196196
self._lock = DeferredLock()
197197

198-
self._logger = get_netqasm_logger(f"{self.__class__.__name__}({name})")
198+
self._logger = logging.getLogger(f"{self.__class__.__name__}({name})")
199199

200200
# Read in topology, if specified. topology=None means fully connected
201201
# topology

simulaqron/network.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@
3434

3535
import networkx as nx
3636
from multiprocess.context import ForkProcess as Process
37-
from netqasm.logging.glob import get_netqasm_logger, get_log_level
37+
import logging
3838

3939
from simulaqron.settings import network_config
4040
from simulaqron.settings.network_config import NodeConfig
41+
from simulaqron.settings import simulaqron_settings
4142
from simulaqron.start import start_vnode, start_qnodeos
4243
# WARNING - this import *needs* to be after importing start_vnode and start_qnodeos
4344
# Otherwise the code that patches some netqasm internal definitions will not work correctly!
@@ -66,7 +67,7 @@ def __init__(self, nodes: List[str], network_name: str = "default"):
6667
self.name = network_name
6768

6869
self.processes: List[Process] = []
69-
self._logger = get_netqasm_logger(f"{self.__class__.__name__}({self.name})")
70+
self._logger = logging.getLogger(f"{self.__class__.__name__}({self.name})")
7071

7172
# Determine the nodes to start, using the in-memory network config
7273
self._nodes_to_start: List[NodeConfig] = []
@@ -110,10 +111,10 @@ def _setup_processes(self):
110111
"""
111112
for node in self._nodes_to_start:
112113
process_virtual = Process(
113-
target=start_vnode, args=(node.name, self.name, get_log_level()), name=f"VirtNode {node.name}"
114+
target=start_vnode, args=(node.name, self.name, simulaqron_settings.log_level), name=f"VirtNode {node.name}"
114115
)
115116
process_qnodeos = Process(
116-
target=start_qnodeos, args=(node.name, self.name, get_log_level()), name=f"QnodeOSNode {node.name}"
117+
target=start_qnodeos, args=(node.name, self.name, simulaqron_settings.log_level), name=f"QnodeOSNode {node.name}"
117118
)
118119
self.processes += [process_virtual, process_qnodeos]
119120

simulaqron/run/run.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from multiprocess.context import ForkContext as ProcessContext
1212
from multiprocess.pool import ApplyResult
1313
from multiprocess.sharedctypes import SynchronizedArray
14-
from netqasm.logging.glob import get_netqasm_logger
14+
import logging
1515
from netqasm.logging.output import (reset_struct_loggers,
1616
save_all_struct_loggers)
1717
from netqasm.runtime import env, process_logs
@@ -28,7 +28,7 @@
2828
from simulaqron.settings import simulaqron_settings, network_config
2929
from simulaqron.settings.simulaqron_config import SimBackend
3030

31-
logger = get_netqasm_logger()
31+
logger = logging.getLogger()
3232

3333
# TODO similar code to squidasm.run.run, make base-class and subclasses?
3434

simulaqron/sdk/connection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
ErrorCode, Message, APP_ID)
1212
from netqasm.lang.ir import GenericInstr
1313
from netqasm.lang.operand import Address, Register
14-
from netqasm.logging.glob import get_netqasm_logger
14+
import logging
1515
from netqasm.sdk import EPRSocket
1616
from netqasm.sdk.config import LogConfig
1717
from netqasm.sdk.connection import BaseNetQASMConnection
@@ -24,7 +24,7 @@
2424
get_node_id_from_net_config)
2525
from simulaqron.settings import network_config
2626

27-
logger = get_netqasm_logger("SimulaQronConnection")
27+
logger = logging.getLogger("SimulaQronConnection")
2828

2929

3030
class SimulaQronConnection(BaseNetQASMConnection):

simulaqron/sdk/socket.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import Optional
44

55
import dill
6-
from netqasm.logging.glob import get_netqasm_logger
6+
import logging
77
from netqasm.sdk.classical_communication.message import StructuredMessage
88
from netqasm.sdk.classical_communication.socket import Socket as _Socket
99

@@ -33,7 +33,7 @@ def __init__(
3333
self._use_callbacks = use_callbacks
3434
self._network_name = network_name
3535

36-
self._logger = get_netqasm_logger(f"{self.__class__.__name__}(L: {app_name} <-> R: {remote_app_name})")
36+
self._logger = logging.getLogger(f"{self.__class__.__name__}(L: {app_name} <-> R: {remote_app_name})")
3737
self._timeout = timeout
3838
# We define _app_socket as None as a default value, so the __del__ method
3939
# does not fail when the socket could not be connected correctly.

simulaqron/sim_util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import numpy as np
2-
from netqasm.logging.glob import get_netqasm_logger
2+
import logging
33
from netqasm.sdk import Qubit
44

55
from simulaqron.sdk import SimulaQronConnection
66

7-
logger = get_netqasm_logger("sim_util")
7+
logger = logging.getLogger("sim_util")
88

99

1010
def get_qubit_state(qubit: Qubit, reduced_dm: bool = True) -> np.ndarray:

simulaqron/simulaqron.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import importlib.metadata as metadata
22
import logging
33
import time
4+
import sys
45
from pathlib import Path
56
from typing import Optional, List
67

@@ -26,23 +27,31 @@
2627
class RunningSimulaQronDaemon(run.RunDaemon):
2728
"""
2829
SimulaQronDaemon class used to represent SimulaQron daemons that are already running.
29-
This class is useful to stop the already-running daemons without needed to read all
30+
This class is useful to 5stop the already-running daemons without needed to read all
3031
the required configurations.
3132
"""
3233

3334
def __init__(self, pidfile: Path):
3435
assert pidfile is not None
35-
super().__init__(pidfile=pidfile)
36-
36+
super().__init__(
37+
pidfile=pidfile,
38+
)
3739

3840
class SimulaQronDaemon(run.RunDaemon):
3941
def __init__(self, pidfile: Path, name: str, nodes: List[str]):
40-
super().__init__(pidfile=pidfile)
42+
super().__init__(
43+
pidfile=pidfile,
44+
)
4145
self.name = name
4246
self.nodes = nodes
4347

4448
def run(self):
4549
"""Starts all nodes defined in netsim's config directory."""
50+
51+
# Let's make sure we can record the output where it's accessible
52+
sys.stdout = open('/tmp/simulaqron.out', 'w', buffering=1)
53+
sys.stderr = open('/tmp/simulaqron.err', 'w', buffering=1)
54+
4655
network = Network(network_name=self.name, nodes=self.nodes)
4756
network.start()
4857

@@ -141,6 +150,7 @@ def stop(name: str):
141150
"""Stops a network."""
142151
assert name is not None
143152
pidfile = PID_FOLDER / f"simulaqron_network_{name}.pid"
153+
logging.debug(f"Trying to open PIDfile")
144154
if not pidfile.exists():
145155
logging.warning("Network with name %s is not running", name)
146156
return
@@ -613,7 +623,8 @@ def get(network_name: str):
613623

614624
if __name__ == "__main__":
615625
logging.basicConfig(
616-
format="%(asctime)s:%(levelname)s:%(message)s",
626+
logging.basicConfig(
627+
format="%(asctime)s:%(levelname)s:%(filename)s:%(lineno)d:%(message)s",
617628
level=simulaqron_settings.log_level,
618-
)
629+
))
619630
cli()

0 commit comments

Comments
 (0)