Skip to content

Commit

Permalink
Cleaning logger
Browse files Browse the repository at this point in the history
 - color cli logs according to level
 - clear file logs from color tags
 - overwrite log file each session
  • Loading branch information
ch-sa committed Feb 6, 2022
1 parent f86d48d commit 48af69c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 42 deletions.
2 changes: 1 addition & 1 deletion labelCloud/control/label_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import List

from ..io.labels import BaseLabelFormat, CentroidFormat, KittiFormat, VerticesFormat
from ..model.bbox import BBox
from ..model import BBox
from .config_manager import config


Expand Down
14 changes: 7 additions & 7 deletions labelCloud/control/pcd_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

import numpy as np
import open3d as o3d
from labelCloud.io.pointclouds.open3d import Open3DHandler

from ..io.pointclouds import BasePointCloudHandler, Open3DHandler
from ..model import BBox, Perspective, PointCloud
from ..utils.logger import green
from ..utils.logger import blue, green, print_column
from .config_manager import config
from .label_manager import LabelManager

Expand Down Expand Up @@ -191,6 +190,7 @@ def rotate_pointcloud(
str(self.pcd_path),
str(originals_path.joinpath(self.pcd_name)),
)
logging.info("Copyied the original point cloud to %s.", blue(originals_path))

# Rotate and translate point cloud
rotation_matrix = o3d.geometry.get_rotation_matrix_from_axis_angle(
Expand All @@ -199,6 +199,10 @@ def rotate_pointcloud(
o3d_pointcloud = Open3DHandler().to_open3d_point_cloud(self.pointcloud)
o3d_pointcloud.rotate(rotation_matrix, center=tuple(rotation_point))
o3d_pointcloud.translate([0, 0, -rotation_point[2]])
logging.info("Rotating point cloud...")
print_column(["Angle:", np.round(angle, 3)])
print_column(["Axis:", np.round(axis, 3)])
print_column(["Point:", np.round(rotation_point, 3)], last=True)

# Check if pointcloud is upside-down
if abs(self.pointcloud.pcd_mins[2]) > self.pointcloud.pcd_maxs[2]:
Expand All @@ -208,12 +212,8 @@ def rotate_pointcloud(
center=(0, 0, 0),
)

save_path = self.pcd_path
# if save_path.suffix == ".bin": # save .bin point clouds as .pcd
# save_path = save_path.parent.joinpath(save_path.stem + ".pcd")

self.pointcloud = PointCloud(
save_path, *Open3DHandler().to_point_cloud(o3d_pointcloud)
self.pcd_path, *Open3DHandler().to_point_cloud(o3d_pointcloud)
)
self.pointcloud.to_file()

Expand Down
2 changes: 1 addition & 1 deletion labelCloud/model/point_cloud.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ctypes
import logging
from pathlib import Path
from typing import Dict, List, Optional, Tuple
from typing import List, Optional, Tuple

import pkg_resources

Expand Down
102 changes: 70 additions & 32 deletions labelCloud/utils/logger.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,85 @@
import logging
import re
import shutil
from enum import Enum
from typing import List

# --------------------------------- FORMATTING -------------------------------- #


class Format(Enum):
RESET = "\033[0;0m"
RED = "\033[1;31m"
GREEN = "\033[0;32m"
YELLOW = "\33[93m" # "\033[33m"
BLUE = "\033[1;34m"
CYAN = "\033[1;36m"
BOLD = "\033[;1m"
REVERSE = "\033[;7m"
HEADER = "\033[95m"
OKBLUE = "\033[94m"
OKCYAN = "\033[96m"
OKGREEN = "\033[92m"
WARNING = "\033[93m"
FAIL = "\033[91m"
ENDC = "\033[0m"
UNDERLINE = "\033[4m"

GREY = "\33[90m"


def format(text: str, color: Format):
return f"{color.value}{text}{Format.ENDC.value}"


red = lambda text: format(text, Format.RED)
green = lambda text: format(text, Format.OKGREEN)
yellow = lambda text: format(text, Format.YELLOW)
blue = lambda text: format(text, Format.BLUE)
bold = lambda text: format(text, Format.BOLD)


class ColorFormatter(logging.Formatter):
MSG_FORMAT = "%(message)s"

FORMATS = {
logging.DEBUG: Format.GREY.value + MSG_FORMAT + Format.ENDC.value,
logging.INFO: MSG_FORMAT,
logging.WARNING: Format.YELLOW.value + MSG_FORMAT + Format.ENDC.value,
logging.ERROR: Format.RED.value + MSG_FORMAT + Format.ENDC.value,
logging.CRITICAL: Format.RED.value
+ Format.BOLD.value
+ MSG_FORMAT
+ Format.ENDC.value,
}

def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)


class UncolorFormatter(logging.Formatter):
MSG_FORMAT = "%(asctime)s - %(levelname)-8s: %(message)s"
PATTERN = re.compile("|".join(re.escape(c.value) for c in Format))

def format(self, record):
record.msg = self.PATTERN.sub("", record.msg)
formatter = logging.Formatter(self.MSG_FORMAT)
return formatter.format(record)


# ---------------------------------- CONFIG ---------------------------------- #

# Create handlers
c_handler = logging.StreamHandler()
f_handler = logging.FileHandler(".labelCloud.log", mode="a")
f_handler = logging.FileHandler(".labelCloud.log", mode="w")
c_handler.setLevel(logging.INFO) # TODO: Automatic coloring
f_handler.setLevel(logging.DEBUG) # TODO: Filter colors

# Create formatters and add it to handlers
f_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)-8s: %(message)s"))
c_handler.setFormatter(ColorFormatter())
f_handler.setFormatter(UncolorFormatter())


logging.basicConfig(
Expand All @@ -21,6 +88,7 @@
handlers=[c_handler, f_handler],
)


# ---------------------------------- HELPERS --------------------------------- #

TERM_SIZE = shutil.get_terminal_size(fallback=(120, 50))
Expand Down Expand Up @@ -50,33 +118,3 @@ def print_column(column_values: List[str], last: bool = False):
for row in rows:
logging.info("".join(str(word).ljust(col_width) for word in row))
rows = []


class Format(Enum):
RESET = "\033[0;0m"
RED = "\033[1;31m"
GREEN = "\033[0;32m"
YELLOW = "\033[33m"
BLUE = "\033[1;34m"
CYAN = "\033[1;36m"
BOLD = "\033[;1m"
REVERSE = "\033[;7m"
HEADER = "\033[95m"
OKBLUE = "\033[94m"
OKCYAN = "\033[96m"
OKGREEN = "\033[92m"
WARNING = "\033[93m"
FAIL = "\033[91m"
ENDC = "\033[0m"
UNDERLINE = "\033[4m"


def format(text: str, color: Format):
return f"{color.value}{text}{Format.ENDC.value}"


red = lambda text: format(text, Format.RED)
green = lambda text: format(text, Format.OKGREEN)
yellow = lambda text: format(text, Format.YELLOW)
blue = lambda text: format(text, Format.BLUE)
bold = lambda text: format(text, Format.BOLD)
2 changes: 1 addition & 1 deletion labelCloud/view/gui.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import re
from pathlib import Path
from typing import TYPE_CHECKING, List, Set
from typing import TYPE_CHECKING, Set

import pkg_resources
from PyQt5 import QtCore, QtGui, QtWidgets, uic
Expand Down

0 comments on commit 48af69c

Please sign in to comment.