<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def log(self, key, val, step_num):
 """Directly log a scalar value to the event file. :param string key: a name for the value :param val: a float :param step_num: the iteration number at which this value was logged """

        try:
            ph, summ = self.summaries[key]
        except KeyError:
            # if we haven't defined a variable for this key, define one
            with self.g.as_default():
                ph = tf.placeholder(tf.float32, (), name=key)  # scalar
                summ = tf.scalar_summary(key, ph)
            self.summaries[key] = (ph, summ)

        summary_str = self.sess.run(summ, {ph: val})
        self.summ_writer.add_summary(summary_str, step_num)
        return val