Giter VIP home page Giter VIP logo

Comments (6)

jlmaurer avatar jlmaurer commented on June 15, 2024 1

A related/perhaps the same issue is relevant error reporting. I've run into issues where mintpy crashes and it turns out to be simple file naming conventions; e.g. underscores vs dashes in a name. Usually the crash point happens well after the actual issue code, so some sort of verbose log file could be helpful when sorting this out.

from mintpy.

pbrotoisworo avatar pbrotoisworo commented on June 15, 2024 1

Would it be useful to capture all print commands and then write it to a log file? I do that in my software. Here is the template I use for my own projects. I use 2 functions where 1 function is used to run the CLI and capture output, the 2nd function writes it to a log file.

I am sharing my CLI tool I use which I got from Whitebox Tools with some revisions for logging.

    def _run_command(self, cmd: str, cwd: Union[str, None]) -> int:
        """
        Run command and log to file
        :param cmd: Command to execute
        :param cwd: Current working directory
        :return: Return code 0 is success. 1 is failure
        """

        # Create file to capture output
        if not os.path.exists(self.log_file):
            with open(self.log_file, 'w', newline='') as f:
                f.write('DINSAR LOG FILE\n')
                f.write(f'TIMESTAMP: {self.timestamp}\n')
                f.write('======================================\n')

        proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT, bufsize=0, universal_newlines=True,
                                cwd=cwd)
        return_code = 0
        while proc is not None:
            try:
                line = proc.stdout.readline()
            except UnicodeDecodeError:
                sys.stdout.flush()
                continue
            sys.stdout.flush()
            if line != '':
                print_line = line.replace('\n', '')
                print(print_line)
                with open(self.log_file, 'a', newline='') as f:
                    f.write(line)
                # Catch errors using string matching
                if 'Error:' in line:
                    msg = f'WARNING: Possible CLI failure'
                    self._print_to_log(msg)
                    return_code = 1
            else:
                break

        return return_code
    def _print_to_log(self, input_var, print_to_terminal=True) -> None:
        """
        Object print command that will also print to the log file
        :return:
        """
        input_var = str(input_var)
        if print_to_terminal:
            print(input_var)
        path = self.log_file
        file_exists = os.path.exists(path)
        with open(path, 'a', newline='') as f:
            if not file_exists:
                f.write('MINTPY LOG FILE\n')
                f.write(f'TIMESTAMP: {self.timestamp}\n')
                f.write('======================================\n')
            if not input_var.endswith('\n'):
                input_var += '\n'
            f.write(input_var)

Here is a sample log file from my own analyses:

image

Using the functions as an example, you will have to replace every print() statement with _print_to_log(). E.g., _print_to_log('Hello world').

If you just want to write it to the log file and not print on the terminal then set print_to_terminal=False.

from mintpy.

yunjunz avatar yunjunz commented on June 15, 2024

@falkamelung I have not seen (2) in other projects, could you give some references?

We should apply proper logging. I would recommend you to find a permanent solution with proper documentation so that other developers could easily follow it.

from mintpy.

falkamelung avatar falkamelung commented on June 15, 2024

This remains an issue

from mintpy.

yunjunz avatar yunjunz commented on June 15, 2024

Since this issue still remains, let's keep it open for future reference.

from mintpy.

yunjunz avatar yunjunz commented on June 15, 2024

@jlmaurer Could you post an example so that we could be more aware of the scenario? That would help shape the future logging style.

from mintpy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.