Skip to content

Commit

Permalink
use penlog for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
peckto committed Aug 1, 2022
1 parent e8b3ee4 commit 7d75203
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 167 deletions.
66 changes: 38 additions & 28 deletions src/gallia/analyzer/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from gallia.analyzer.config import SrcPath
from gallia.analyzer.mode_config import LogMode, ScanMode, OpMode
from gallia.analyzer.name_config import ColNm, KyNm, TblNm, VwNm, NEG_STR
from gallia.utils import g_repr


class Analyzer(Operator):
Expand All @@ -34,7 +35,6 @@ def __init__(
debug_on: bool = False,
):
Operator.__init__(self, path, log_mode)
self.msg_head = "[Analyzer] "
self.debug_on = debug_on
self.debug_dir = artifacts_dir.joinpath("debug")
if debug_on:
Expand All @@ -60,7 +60,7 @@ def analyze_each_run(self, run: int, op_mode: OpMode) -> bool:
"""
analyze certain run at a given operation mode.
"""
self.log(f"analyzing run #{str(run)} from {self.db_path} ...")
self.logger.log_summary(f"analyzing run #{str(run)} from {self.db_path} ...")
scan_mode = self.get_scan_mode(run)
if scan_mode == ScanMode.SERV:
if not self.reset(TblNm.serv, run):
Expand All @@ -83,7 +83,7 @@ def reset(self, table_name: str, run: int) -> bool:
self.cur.executescript(reset_sql)
self.con.commit()
except (OperationalError, FileNotFoundError, KeyError) as exc:
self.log("reseting analysis in place failed", True, exc)
self.logger.log_error(f"resetting analysis in place failed: {g_repr(exc)}")
return False
return True

Expand All @@ -110,7 +110,9 @@ def analyze_serv(self, run: int, op_mode: OpMode) -> bool:
)
analyze_sql += update_sql
except KeyError as exc:
self.log("condition key reading failed", True, exc)
self.logger.log_error(
f"condition key reading failed: {g_repr(exc)}"
)
if self.debug_on:
path = self.debug_dir.joinpath(f"analyze_serv_{str(run)}.sql")
with path.open("w", encoding="utf8") as file:
Expand All @@ -127,7 +129,9 @@ def analyze_serv(self, run: int, op_mode: OpMode) -> bool:
JSONDecodeError,
NotImplementedError,
) as exc:
self.log("analyzing scan_service in place failed", True, exc)
self.logger.log_error(
f"analyzing scan_service in place failed: {g_repr(exc)}"
)
return False
return True

Expand All @@ -137,7 +141,9 @@ def analyze_iden(self, run: int, op_mode: OpMode) -> bool:
without using data frame direct in data base.
"""
if op_mode == OpMode.ISO:
self.log("ISO Standard analysis unavailable for scan_identifier.", True)
self.logger.log_warning(
"ISO Standard analysis unavailable for scan_identifier"
)
return False
self.prepare_alwd_res()
try:
Expand Down Expand Up @@ -185,8 +191,10 @@ def analyze_iden(self, run: int, op_mode: OpMode) -> bool:
analyze_sql += update_sql
else:
pass
except (KeyError) as exc:
self.log("condition key reading failed", True, exc)
except KeyError as exc:
self.logger.log_error(
f"condition key reading failed: {g_repr(exc)}"
)
drop_view_sql = f"""
DROP VIEW IF EXISTS "{VwNm.sess_alwd}";
DROP VIEW IF EXISTS "{VwNm.sbfn_alwd}";
Expand All @@ -209,7 +217,9 @@ def analyze_iden(self, run: int, op_mode: OpMode) -> bool:
AttributeError,
JSONDecodeError,
) as exc:
self.log("analyzing scan_identifier in place failed", True, exc)
self.logger.log_error(
f"analyzing scan_identifier in place failed: {g_repr(exc)}"
)
return False
return True

Expand All @@ -225,8 +235,10 @@ def interpret(
cond = ""
try:
failure = self.fail_name_dict[cond_dict[KyNm.fail]]
except (KeyError) as exc:
self.log("getting failure condition from JSON failed", True, exc)
except KeyError as exc:
self.logger.log_error(
f"getting failure condition from JSON failed: {g_repr(exc)}"
)
return 255, ""

if KyNm.match in cond_dict.keys():
Expand Down Expand Up @@ -304,11 +316,9 @@ def get_fail_cond_match(
f""" AND ({ref_cols}) {neg_str}IN """
+ f"""(SELECT({ref_cols} ) FROM "{VwNm.ref_vw}")"""
)
except (KeyError) as exc:
self.log(
f"condition key reading failed at '{neg_str}{KyNm.match}'",
True,
exc,
except KeyError as exc:
self.logger.log_error(
f"condition key reading failed at '{neg_str}{KyNm.match}': {g_repr(exc)}"
)
add_cond = ""
return cond + " " + textwrap.dedent(add_cond)
Expand All @@ -326,9 +336,9 @@ def get_fail_cond_resp(self, cond: str, cond_dict: dict, neg: bool = False) -> s
else:
add_cond += str(self.iso_err_name_dict[resp_name]) + ","
add_cond = add_cond[:-1] + ")"
except (KeyError) as exc:
self.log(
f"condition key reading failed at '{neg_str}{KyNm.resd}'", True, exc
except KeyError as exc:
self.logger.log_error(
f"condition key reading failed at '{neg_str}{KyNm.resd}': {g_repr(exc)}"
)
add_cond = ""
return cond + " " + textwrap.dedent(add_cond)
Expand Down Expand Up @@ -403,9 +413,9 @@ def get_fail_cond_supp(
+ f"""FROM "{TblNm.ref_resp}")"""
)
cond += add_cond
except (KeyError) as exc:
self.log(
f"condition key reading failed at '{neg_str}{KyNm.supp}'", True, exc
except KeyError as exc:
self.logger.log_error(
f"condition key reading failed at '{neg_str}{KyNm.supp}': {g_repr(exc)}"
)
return cond

Expand All @@ -424,9 +434,9 @@ def get_fail_cond_for_serv(
else:
add_cond += str(self.iso_serv_name_dict[serv_name]) + ","
add_cond = add_cond[:-1] + ")"
except (KeyError) as exc:
self.log(
f"condition key reading failed at '{neg_str}{KyNm.for_serv}'", True, exc
except KeyError as exc:
self.logger.log_error(
f"condition key reading failed at '{neg_str}{KyNm.for_serv}': {g_repr(exc)}"
)
return cond
return cond + " " + add_cond
Expand Down Expand Up @@ -455,8 +465,8 @@ def get_fail_cond_known(self, cond: str, cond_dict: dict, neg: bool = False) ->
for resp in self.iso_err_code_vec:
add_cond += str(resp) + ","
cond += add_cond[:-1] + ")"
except (KeyError) as exc:
self.log(
f"condition key reading failed at '{neg_str}{KyNm.known}'", True, exc
except KeyError as exc:
self.logger.log_error(
f"condition key reading failed at '{neg_str}{KyNm.known}': {g_repr(exc)}"
)
return cond
32 changes: 19 additions & 13 deletions src/gallia/analyzer/categorizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from gallia.analyzer.name_config import ColNm, TblNm
from gallia.analyzer.exceptions import EmptyTableException, ColumnMismatchException
from gallia.uds.core.constants import UDSIsoServices, UDSErrorCodes
from gallia.utils import g_repr


class Categorizer(Analyzer):
Expand All @@ -30,7 +31,6 @@ def __init__(
self, path: str, artifacts_dir: Path, log_mode: LogMode = LogMode.STD_OUT
):
Analyzer.__init__(self, path, artifacts_dir, log_mode)
self.msg_head = "[Categorizer] "

def analyze_serv(self, run: int, op_mode: OpMode) -> bool:
"""
Expand All @@ -45,7 +45,7 @@ def analyze_serv(self, run: int, op_mode: OpMode) -> bool:
if not self.write_db(raw_df, TblNm.serv):
return False
except (EmptyTableException, ColumnMismatchException, OperationalError) as exc:
self.log("analyzing scan_service failed", True, exc)
self.logger.log_error(f"analyzing scan_service failed: {g_repr(exc)}")
return False
return True

Expand All @@ -65,7 +65,7 @@ def analyze_iden(self, run: int, op_mode: OpMode) -> bool:
if not self.write_db(raw_df, TblNm.iden):
return False
except (EmptyTableException, ColumnMismatchException, OperationalError) as exc:
self.log("analyzing scan_identifier failed", True, exc)
self.logger.log_error(f"analyzing scan_identifier failed: {g_repr(exc)}")
return False
return True

Expand All @@ -88,8 +88,10 @@ def categorize_serv(
lambda x: self.get_fail_serv(op_mode, x[0], x[1], x[2], x[3])
)
raw_df = raw_df.drop([ColNm.combi], axis=1)
except (KeyError) as exc:
self.log("categorizing failures for scan_service failed", True, exc)
except KeyError as exc:
self.logger.log_error(
f"categorizing failures for scan_service failed: {g_repr(exc)}"
)
return pd.DataFrame()
return raw_df

Expand All @@ -105,7 +107,7 @@ def categorize_iden(
try:
serv_vec = np.unique(raw_df[ColNm.serv])
if not serv_vec.size == 1:
self.log("more than one service in a run", True)
self.logger.log_error("more than one service in a run")
return pd.DataFrame()
else:
serv = serv_vec[0]
Expand All @@ -127,8 +129,10 @@ def categorize_iden(
)
)
raw_df = raw_df.drop([ColNm.combi], axis=1)
except (KeyError) as exc:
self.log("categorizing failures for scan_identifier failed", True, exc)
except KeyError as exc:
self.logger.log_error(
f"categorizing failures for scan_identifier failed: {g_repr(exc)}"
)
return pd.DataFrame()
return raw_df

Expand Down Expand Up @@ -233,7 +237,7 @@ def get_fail_serv(
return Failure.OK_SERV_B

if not cond_serv_supp:
# normal responses to unsupporeted services
# normal responses to unsupported services
if cond_resp_means_not_supp:
return Failure.OK_SERV_C

Expand All @@ -252,7 +256,7 @@ def get_fail_serv(
if cond_resp_means_not_supp:
return Failure.OK_SERV_E

# Undocumented Type B: supported servcies in not available session responded
# Undocumented Type B: supported services in not available session responded
# other than "not supported" family
if not cond_resp_means_not_supp:
return Failure.UNDOC_SERV_B
Expand All @@ -262,7 +266,7 @@ def get_fail_serv(
if cond_resp_alwd and not cond_resp_means_not_supp:
return Failure.OK_SERV_F

# supported servcies (and even in available session) give a response undocumented in ISO
# supported services (and even in available session) give a response undocumented in ISO
if not cond_resp_means_not_supp:
return Failure.OK_SERV_G

Expand All @@ -276,7 +280,7 @@ def get_fail_serv(
if cond_resp_serv_not_supp:
return Failure.MISS_SERV_B

# supported services in available session give a reponded as "subFunctionNotSupported"
# supported services in available session give a responded as "subFunctionNotSupported"
if cond_resp_sbfn_not_supp:
return Failure.OK_SERV_H

Expand Down Expand Up @@ -328,7 +332,9 @@ def get_fail_iden(
break

except (KeyError, AttributeError) as exc:
self.log("getting failure for identifier failed", True, exc)
self.logger.log_error(
f"getting failure for identifier failed: {g_repr(exc)}"
)
return Failure.UNKNOWN

if cond_combi:
Expand Down
Loading

0 comments on commit 7d75203

Please sign in to comment.