<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def generate_changelog(issues, after, heading, rtag=False):
 """Prints out changelog."""

    relevent = relevant_issues(issues, after)
    relevent = sorted(relevent, key=ISSUES_BY_SECTION)

    def write(func, endofline="", append=""):
        func(heading + '\n' + '-' * 20 + endofline)
        for section, issue_group in groupby(relevent, key=ISSUES_BY_SECTION):
            func('  * {}:'.format(section) + endofline)
            for issue in reversed(list(issue_group)):
                func('    - {}'.format(issue_line(issue)) + endofline)
        func(endofline + append)

    if rtag is not False:
        with open("../CHANGELOG", "r+") as f:
            content = f.read()
            f.seek(0)
            write(f.write, '\n', content)
    else:
        write(print)